Several ways PHP generates random passwords

  • 2020-03-31 21:26:03
  • OfStack

Random password is a string of fixed length, here I collected several methods to generate random string, for your reference.Method one:

          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; 
} 

Method 3:

          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 = array_rand($chars, $length); 

$password = ''; 
for($i = 0; $i < $length; $i++) 
{ 
//Concatenate $length array elements into strings
$password .= $chars[$keys[$i]]; 
} 

return $password; 
} 

Method 4:
This method is the article is blue after the ideal reprint, a net friend provided a new method, algorithm is simple, the code is short, just because of the return value of md5() function, the generated password only includes letters and Numbers, but also a good method. Algorithm idea:
1. Time () gets the current Unix timestamp
2. Md5 () encrypt the timestamp obtained in the first step
3, the results of the second step of encryption, intercept n bits to get the desired password
 
function get_password( $length = 8 ) 
{ 
$str = substr(md5(time()), 0, 6); 
return $str; 
} 

Time efficiency comparison
Using the following PHP code, we calculate the elapsed time for each of the four 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
Method 4:3.4093856811523E-5 seconds

It can be seen that method 1 and method 2 have the same execution time, method 4 has the shortest running time, and method 3 has a slightly longer running time.


Related articles: