<?php
function random_password($length = 6)
{
$characters = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
shuffle($characters);
return implode('', array_slice($characters, 0, $length));
}
echo random_password();
?>
Refactorings
No refactoring yet !
jmut
October 17, 2007, October 17, 2007 15:45, permalink
Seems this is limited to 62 char long password. I will just add more randomness in it :)
function random_password($length = 6)
{
$characters = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
srand((float)microtime() * 1000000);
shuffle($characters);
return implode('', array_slice($characters, 0, $length));
}
echo random_password();
richardhealy
October 17, 2007, October 17, 2007 16:15, permalink
Not sure what the speed differences are between using Arrays vs While statements but here's something a little different. This only uses lowercase letters though.
[php]
<?php
function random_password($length = 6)
{
while (strlen($key) < $length) $key .= rand(0,1) ? chr(rand(48, 57)) : chr(rand(97, 122));
return $key;
}
echo random_password();
?>
Meir
October 17, 2007, October 17, 2007 16:46, permalink
My new code is faster, a simple benchmark test:
Meir:
<?php
function random_password($length = 6)
{
$characters = implode('', array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
$characters = str_repeat(str_shuffle($characters), $length > 62 ? ceil($length/62) : 1);
return substr($characters, 0, $length);
}
$start = array_sum(explode(' ', microtime()));
for ($i = 0; $i < 10000; $i++) {
random_password(100);
}
$end = array_sum(explode(' ', microtime()));
$final = $end - $start;
echo number_format($final, 4, '.', '').' seconds'; // 0.5664 seconds
?>
richardhealy:
<?php
function random_password($length = 6)
{
while (strlen($key) < $length) $key .= rand(0,1) ? chr(rand(48, 57)) : chr(rand(97, 122));
return $key;
}
$start = array_sum(explode(' ', microtime()));
for ($i = 0; $i < 10000; $i++) {
random_password(100);
}
$end = array_sum(explode(' ', microtime()));
$final = $end - $start;
echo number_format($final, 4, '.', '').' seconds'; // 3.3504 seconds
?>
richardhealy
October 17, 2007, October 17, 2007 18:18, permalink
I suppose that answers that question then! lol! Interesting to know, I'm going to change my random code generator in my apps now on the back of this. Cheers Meir! :)
Random
October 18, 2007, October 18, 2007 15:02, permalink
function randomString($length = 8, $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
{
$string = '';
$max = strlen($characters) - 1;
for($i = 0; $i < $length; ++$i) {
$string .= $characters[mt_rand(0, $max)];
}
return $string;
}
Felix
October 19, 2007, October 19, 2007 16:07, permalink
The easy way.
<?php
function random_password($length = 8){
return substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'),0,$length);
}
?>
Ted
October 26, 2007, October 26, 2007 12:06, permalink
The problem with these scripts above is that they don't produce a strong password. A password with a length of < 64 charachters will always have 0 or max 1 of each charachter. Passwords like 'aaBBcc' will never occure.
The code below is slower, but generates a more unique password.
function random_password($length = 6)
{
$pass = '';
for($n =0; $n < $length; $n++)
$pass .= substr('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', rand(0, 63), 1);
return $pass;
}
Eineki
November 7, 2007, November 07, 2007 10:08, permalink
I'm using this function for random passing generation and I'm quite satisfied of it
Can you see any weakness?
function random_password($length = 6)
{
return substr( md5(microtime()),0, $length);
}
Gerry
August 16, 2008, August 16, 2008 16:49, permalink
Slight rework of Ted's code.
Main change is using mt_rand() instead of rand(), which is probably the cause of the speed issues which he mentioned.
<?php
function randomPassword($length = 6){
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$charsLen = strlen($chars)-1;
$pass = '';
for($n =0; $n < $length; $n++){
$pass .= substr($chars, mt_rand(0, $charsLen), 1);
}
return $pass;
}
?>
Just Another Programmer
December 19, 2008, December 19, 2008 05:10, permalink
This one uses special characters also, and shuffles it all around etc..
<?php
function randomPassword($length) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!@#$%^&*(){}[]|~';
$charsarray = array();
for ($n=0; $n < rand(0,rand(0,100)); $n++) {
$charsarray[] = str_shuffle($chars);
}
$password = substr(str_shuffle(str_shuffle(implode($charsarray))),rand(0,strlen($password))-$length,$length);
return $password;
}
echo randomPassword(12);
//Sample generated password: tY$%bSzzU!e[
?>
:)