4 ways to generate random passwords in PHP and performance comparisons
Using PHP to develop applications, especially Web applications, often requires the generation of random passwords, such as user registration to generate random passwords, user reset password also need to generate a random password. Random password is a string of fixed length, here I collected several methods to generate random strings, for your reference.
Method One:
1, generate a random integer in the 33–126, such as 35,
2, convert 35 to the corresponding ASCII character, such as 35 corresponding #
3, repeat the above 1, 2 steps n times, connected to n-bit password
The algorithm mainly uses two functions, Mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max the range of ASCII code, here take 33-126, you can adjust the range as necessary, such as the ASCII code table 97– 122 digits corresponding to the a–z of the English alphabet, specific reference to the ASCII code table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.
View Sourceprint?
- function Create_password ($pw _length = 8)
- {
- $randpwd = '' ;
- for ($i = 0; $i < $PW _length ; $i ++)
- {
- $randpwd .= CHR (Mt_rand (126));
- }
- return $randpwd;
- }
- //Call this function, pass length parameter $pw_length = 6
- Echo Create_password (6);
Method Two:
1. Preset a string $chars, including a–z,a–z,0–9, and some special characters
2. Randomly take a character in a $chars string
3, repeat the second step n times, can get the length of the password of n
View Sourceprint?
- function Generate_password ( $length = 8) {
- //password character set, you can add any character you need
- $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| ' ;
- $password = '' ;
- for ( $i = 0; $i < $length ; $i ++ )
- {
- //Here are two ways to get a character
- //The first is to use SUBSTR to intercept any one character in the $chars;
- //The second is any element that takes a character array $chars
- //$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
- $password .= $chars [Mt_rand (0, strlen($chars)-1)];
- }
- return $password;
- }
Method Three:
1, preset a character array $chars, including a–z,a–z,0–9, and some special characters
2. Randomly select $length elements from the array $chars by Array_rand ()
3, according to the obtained key an array group $keys, from the array $chars remove the character stitching string. The disadvantage of the method is that the same characters are not repeated.
View Sourceprint?
- function Make_password ( $length = 8)
- {
- //password character set, you can add any character you need
- $chars = Array (' a ', ' B ', ' C ', ' d ', ' e ', ' F ', ' G ', ' h ',
- ' I ' , ' J ' , ' K ' , ' l ' , ' m ' , ' n ' , ' O ' , ' P ' , ' Q ' , ' R ' , ' s ' ,
- , ' u ' , ' V ' , ' W ' , ' x ' , ' y ' , ' Z ' , ' A ' , ' B ' , ' C ' , ' D ' ,
- ' E ' , ' F ' , , ' H ' , ' I ' , ' J ' , ' K ' , ' L ' , ' M ' , ' N ' , ' O ' ,
- , ' Q ' , , ' S ' , ' T ' , ' U ' , ' V ' , ' W ' , ' X ' , ' Y ' , ' Z ' ,
- ' 0 ' , ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' , ' 6 ' , ' 7 ' , ' 8 ' , ' 9 ' , '! ' ,
- ' @ ' , ' # ' , ' $ ' , , ' ^ ' , ' & ' , ' * ' , , , -' , ,
- '[', ']' , '{', '}' , ' < ' , ' > ' , '~' , '`' , '+' , '=' , ',' ,
- '.' , ';' , ':' , '/' , '?' , '|' );
- //$length number of element key names randomly taken in the $chars
- $keys = Array_rand ($chars, $length);
- $password = '' ;
- for ($i = 0; $i < $length ; $i ++)
- {
- //concatenate $length array elements into a string
- $password .= $chars [$keys[$i]];
- }
- return $password;
- }
Method Four:
This method is reproduced in this article by the blue Ideal, a netizen provides a new method, the algorithm is simple, the code is short, just because the MD5 () function of the return value of the reason, the generated password only includes letters and numbers, but also a good way. Algorithm idea:
1. Time () Gets the current Unix timestamp
2, the first step to obtain the timestamp for MD5 () encryption
3, the second step to encrypt the result, intercept n-bit is the desired password
View Sourceprint?
- function$length = 8)
- $strsubstr
- return$str
Time Efficiency comparison
We use the following PHP code, calculate the above 4 random password generation function generated 6-bit password run time, and then a simple comparison of their time efficiency.
View Sourceprint?
-
- function getmicrotime ()
- {
- List ($usec, $sec) = explode("", Microtime ());
- return (float)$usec + (float)$sec);
- }
- //Record start time
- $time _start = Getmicrotime ();
- //Put the PHP code to execute here, such as:
- //Echo Create_password (6);
- //Record end time
- $time _end = Getmicrotime ();
- $time = $time _end - $time _start ;
- //Output run total time
- Echo "Execution time $time seconds";
- ?>
The final result is:
Method One: 9.8943710327148E-5 seconds
Method Two: 9.6797943115234E-5 seconds
Method Three: 0.00017499923706055 seconds
Method Four: 3.4093856811523E-5 seconds
It can be seen that the execution time of method one and method two is similar, the method four run time is shortest, and the running time of method Three is a little longer.
http://www.bkjia.com/PHPjc/994105.html www.bkjia.com true http://www.bkjia.com/PHPjc/994105.html techarticle PHP 4 Methods for generating random passwords and performance comparison using PHP to develop applications, especially website programs, often need to generate random passwords, such as user registration to generate random passwords, user heavy ...