Summary of three ways PHP generates random passwords
- 2020-03-31 21:04:37
- OfStack
use PHP Developing applications, especially web applications, is often required Generate random password , if the user registers to generate a random password, the user resets the password to also generate a random password. Random password is a string of fixed length, here I collected several methods to generate random string, for your reference.
Methods a :
1. Generate a random integer in 33 and 126, such as 35,
2. Convert 35 to the corresponding ASCII code character, such as #
3. Repeat steps 1 and 2 above for n times to connect to an n-bit password
The algorithm USES two main functions, (link: #) function is used to generate random integers, where $$Max min � for the scope of the ASCII here take 33-126, can according to need to adjust the scope, such as ASCII code table 97 � 122 English letters, corresponding to a � z specific reference (link: http://www.asciitable.com/); The (link: #) function converts the corresponding integer $ASCII to the corresponding character.
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
//Call this function, passing the length parameter $pw_length = 6
echo create_password(6);
Method 2:
1. Preset a string $chars, including a & z, a & z, 0 & 9, and some special characters
2. Pick a random character in the $chars string
3. Repeat the second step n times to get the password of length n
function generate_password( $length = 8 ) {
//Password character set, you can add any character you want
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
//There are two ways to get characters
//The first is to use substr to intercept any character in $chars.
//The second is to take any element of the character array $chars
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
Methods three :
1. Preset a character array $chars, including a and z, a and z, 0 and 9, as well as some special characters
2. Randomly select $length elements from the array $chars by (link: #)
3. Extract the character concatenation string from the array $chars according to the obtained array $keys. The disadvantage of this method is that the same characters are not retrieved repeatedly.
function make_password( $length = 8 )
{
//Password character set, you can add any character you want
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!',
'@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
'[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',',
'.', ';', ':', '/', '?', '|');
//Randomly pick $length array element keys in $chars
$keys = ($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
//Concatenate $length array elements into strings
$password .= $chars[$keys[$i]];
}
return $password;
}
Time efficiency comparison
Using the following PHP code, we calculate the elapsed time for each of the three random password-generating functions above to generate a 6-bit password, to provide a simple comparison of their time efficiency.
<?php
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
//Record start time
$time_start = getmicrotime();
//Here is the PHP code to execute, such as:
// echo create_password(6);
//Record end time
$time_end = getmicrotime();
$time = $time_end - $time_start;
//Output total run time
echo " The execution time $time seconds";
?>
The final result is:
Method 1:9.8943710327148E-5 seconds
Method 2:9.6797943115234E-5 seconds
Method 3:0.00017499923706055 seconds
You can see that method 1 and method 2 both take about the same amount of time to execute, while method 3 takes a little longer.
Original text: (link: http://www.ludou.org/how-to-create-a-password-generator-using-php.html)