Generator random string (password)

function _RandomString($length=10,$uc=TRUE,$n=TRUE,$sc=FALSE){ $source = 'abcdefghijklmnopqrstuvwxyz'; if($uc==1) $source .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if($n==1) $source .= '1234567890'; if($sc==1) $source .= '|@#~$%()=^*+[]{}-_'; if($length>0){ $rstr = ""; $source = str_split($source,1); for($i=1; $i<=$length; $i++){ mt_srand((double)microtime() * 1000000); $num = mt_rand(1,count($source)); $rstr .= $source[$num-1]; } } return $rstr; }

1 Response

Wouldn't any of these, or something similar, be a little easier?

$random = mt_rand(0, 999999);
$random_string = sha1($random);

// or

$random = file_get_contents('/dev/urandom', false, null, 0, 10);

// or

$string = bin2hex($random);
$number = current(unpack('L', $random));

// or

mcrypt_create_iv(10, MCRYPT_DEV_URANDOM);

// or

$salt = mcrypt_create_iv(10, MCRYPT_DEV_URANDOM);
$salt = base64_encode($salt);
$salt = str_replace('+', '.', $salt);
$hash = crypt('rasmuslerdorf', '$2y$10$'.$salt.'$');

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.