NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Random Passwords

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Reply
 
LinkBack Thread Tools
Old 11-03-2007, 12:36 PM THREAD STARTER               #1 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics

Random Passwords


Got a tad bored today, so wrote a function for random passwords. Maybe it will help someone.

PHP Code:
<?php

/**
* Generate a random password.
*
* @param  integer  $numchars      How long do we need the password to be?
* @param  boolean  $specialchars  Include the special characters?
* @param  boolean  $extrashuffle  Include an extra randomization on the password string?
* @return string
*/
function random_pass($numchars 8$specialchars true$extrashuffle false)
????: NamePros.com http://www.namepros.com/code/391711-random-passwords.html
{
    
$numchars intval($numchars);
    
$numchars = ($numchars 16 OR $numchars 8) ? $numchars;

    
$chars array_merge(range('a''z'), range(09));

    if (
$specialchars)
    {
        
$chars array_merge($chars, array('!''$''_''-''#''@'));
    }
    
shuffle($chars);

    
$pass '';

    for (
$i 0$i <= $numchars$i++)
    {
        
$pass .= $chars[$i];
    }

    if (
$extrashuffle)
    {
        return 
str_shuffle($pass);
    }
    return 
$pass;
}

// Example, returns: 3ck#4sib2
echo random_pass(8truetrue);
????: NamePros.com http://www.namepros.com/showthread.php?t=391711

?>
Eric is offline   Reply With Quote
Old 11-03-2007, 01:31 PM   #2 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
PHP Code:
  <?php

/**
* Generate a random password.
*
* @param  integer  $numchars      How long do we need the password to be?
* @param  boolean  $specialchars  Include the special characters?
* @param  boolean  $extrashuffle  Include an extra randomization on the password string?
* @param  boolean  $mixedcase     Mixed case or solely lowercase?
* @return string
*/
function random_pass($numchars 8$specialchars true$extrashuffle false$mixedcase true)
{
    
$numchars intval($numchars);
    
$numchars = ($numchars 16 OR $numchars 8) ? $numchars;
    
    
$chars array_merge(range('a''z'), range(09));

    if (
$mixedcase/* Extra case */
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
    
{
        
$chars array_merge($charsrange('A','Z'));
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
    }
    
    if (
$specialchars)
    {
        
$chars array_merge($chars, array('!''$''_''-''#''@'));
    }
    
shuffle($chars);

    
$pass '';

    for (
$i 0$i $numchars$i++) /* Changed to < only sign, otherwise it would be 1 character longer than needed */
    
{
        
$pass .= $chars[$i];
        
shuffle($chars); /* Get repeated characters, would add as an option, but I'm lazy */
    
}

    if (
$extrashuffle)
    {
        return 
str_shuffle($pass);
    }
    return 
$pass;
}

// Example, returns: 3ck#4si2
echo random_pass(8truetruetrue);

?>
Added an extra shuffle after each character, so you can get repeated characters too. (I prefer it that way).
Added MiXeD case option
Fixed the length ($i = 0; $i <=$numchars) part.
Last edited by Danltn; 11-03-2007 at 01:36 PM.
Daniel is offline   Reply With Quote
Old 11-04-2007, 10:22 AM   #3 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Both good, but heres a function I made a while ago and have been using, it may not be as customizable, but the passwords are very difficult to guess:
PHP Code:
<?php
function prand($len 10){
     
$pass '';
     for(
$i 0$i $len$i ++){
          
$pass .= chr(rand(33126));
     }
     return 
$pass;
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
}

echo 
prand();
?>
Barrucadu is offline   Reply With Quote
Old 02-05-2008, 04:42 PM   #4 (permalink)
New Member
Join Date: Feb 2008
Posts: 9
jimmysanders is an unknown quantity at this point
 



i had to go for the smallest one

PHP Code:
function randomPassword($length 8) {
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
    return    
substr(base64_encode(md5(time())), 0$length);


creates random string of A–Z, a–z, and 0–9 of max of 42ish chars
jimmysanders is offline   Reply With Quote
Old 02-18-2008, 06:53 PM   #5 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
Originally Posted by jimmysanders
i had to go for the smallest one
????: NamePros.com http://www.namepros.com/showthread.php?t=391711

PHP Code:
function randomPassword($length 8) {
    return    
substr(base64_encode(md5(time())), 0$length);


creates random string of A–Z, a–z, and 0–9 of max of 42ish chars
lol protection much...base64 and md5 encryption lol
unknowngiver is offline   Reply With Quote
Old 02-19-2008, 03:24 AM   #6 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
Originally Posted by unknowngiver
Originally Posted by jimmysanders
i had to go for the smallest one
????: NamePros.com http://www.namepros.com/showthread.php?t=391711

PHP Code:
function randomPassword($length 8) {
    return    
substr(base64_encode(md5(time())), 0$length);


creates random string of A–Z, a–z, and 0–9 of max of 42ish chars
lol protection much...base64 and md5 encryption lol
Not very secure, if you knew say what time roughly they made it, and how long, it could easily be cracked.

Dan
Daniel is offline   Reply With Quote
Old 07-10-2008, 09:21 PM   #7 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
If you know exactly what second after Unix Epoch they made that password, then yes... you could guess it...
????: NamePros.com http://www.namepros.com/showthread.php?t=391711

Unless we add a little bit of salt

PHP Code:
// Modified function, original given by jimmysanders

function randomPassword($length 8) {
    
$saltLength 1;
    return    
substr(base64_encode(md5(time())), 0$length $saltLength) . substr(md5($length $time), 5$saltLength);

// To use for your own, change:
// 5 on the second substr() function such that it's > 0 and < 42.
// $saltLength should be < $length
// If each person who used this function changed these values to
// some of their own choice, guessing it by just time() would be much trickier.

That should be a little trickier.
Last edited by mholt; 07-10-2008 at 09:49 PM.
mholt is offline   Reply With Quote
Old 12-12-2008, 06:58 PM   #8 (permalink)
New Member
Join Date: Dec 2008
Posts: 3
Cavemaneca is an unknown quantity at this point
 



This is what I've been using for any random string

PHP Code:
function randstr($l$sp 'false') {
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
$useChars='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz?!-+*=/\|&@#%^';
$string $useChars{mt_rand(0,9)};
for(
$i $i $l $i++)
{
    if (
$sp) {
    
$string .= $useChars{mt_rand(0,76)};
    }
    else {
    
$string .= $useChars{mt_rand(0,61)};
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
    }
}
return 
$string;

usually works out fine. I don't know much encryption though. For that I usually just use
PHP Code:
crypt($user_input$db_pass); 
Last edited by Cavemaneca; 12-12-2008 at 07:07 PM.
Cavemaneca is offline   Reply With Quote
Old 12-13-2008, 11:16 AM   #9 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,556
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
I use this:
PHP Code:
function rand_string($len$chars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
    
$string '';
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
    for (
$i 0$i $len$i++)
    {
        
$pos rand(0strlen($chars)-1);
        
$string .= $chars{$pos};
    }
    return 
$string;

sdsinc is offline   Reply With Quote
Old 12-13-2008, 11:20 AM   #10 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
PHP Code:
function rand_string($len$chars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
????: NamePros.com http://www.namepros.com/showthread.php?t=391711
{
    
$string '';
    
$strlen strlen($chars)-1;
    for (
$i 0$i $len$i++)
    {
        
$pos rand(0$strlen);
        
$string .= $chars{$pos};
    }
    return 
$string;

Slightly optimized

~

And @Cavemaneca

(bool) $sp = 'false' evals to TRUE, not FALSE.

Just be careful with this, as it can definitely fall into unexpected behaviour (although it's not really all that 'unexpected'.

PHP Code:
var_dump( (bool) 'false' ); // Returns bool (true) 
Daniel is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 11:32 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger