4 methods and performance comparison of PHP generating random passwords

4 methods and performance comparison of PHP generating random passwords

method one:

1. Generate a random integer in 33-126, such as 35,

2. Convert 35 into the corresponding ASCII code character, such as 35 corresponds to #

3. Repeat the above steps 1 and 2 n times to connect to an n-digit password

The algorithm mainly uses two functions. The mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max is the range of ASCII code, here is 33 -126, and the range can be adjusted as needed. For example, the 97-122 bits in the ASCII code table correspond to the English letters of a-z. For details, please refer to the ASCII code table; the chr (int $ascii) function is used to convert the corresponding integer $ascii into 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 the function, pass the 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 from the $chars string

3. Repeat the second step n times to get a password of length n

function generate_password( $length = 8) {//Password character set, you can add any characters you need $chars ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=, .;:/?|'; $password =''; for ($i = 0; $i <$length; $i++) {//Here are two ways to obtain characters//The first is to use substr to intercept $ Any one 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 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 through array_rand()

3. According to the acquired key name array $keys, take out the character concatenation string from the array $chars. The disadvantage of this method is that the same character will not be taken repeatedly.

function make_password( $length = 8) {//Password character set, you can add any characters you need $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 take $length array element key names in $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;},'*','(',')','-','_','[',']','{','}','<','>','~', ' `','+','=',',','.',';',':','/','?','|');//randomly select $length in $chars Array element keys $keys = array_rand($chars, $length); $password =''; for($i = 0; $i <$length; $i++) {//concatenate $length array elements into String $password .= $chars[$keys[$i]];} return $password;},'*','(',')','-','_','[',']','{','}','<','>','~', ' `','+','=',',','.',';',':','/','?','|');//randomly select $length in $chars Array element keys $keys = array_rand($chars, $length); $password =''; for($i = 0; $i <$length; $i++) {//concatenate $length array elements into String $password .= $chars[$keys[$i]];} return $password;}|');//Randomly take $length array element key names in $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;}|');//Randomly take $length array element key names in $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 a new method provided by a netizen after this article was reprinted by Blue Ideal. The algorithm is simple and the code is short. It is only because of the return value of the md5() function that the generated password only includes letters and numbers, but it is also considered to be A good way. Algorithm idea:

1, time() Get the current Unix timestamp

2. Encrypt the timestamp obtained in the first step with md5()

3. Intercept the result of the second step of encryption and intercept n bits to get the desired password

function get_password( $length = 8) {$str = substr(md5(time()), 0, 6); return $str;}

Time efficiency comparison

We use the following PHP code to calculate the running time of the above 4 random password generation functions to generate a 6-digit password, and then make 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();//Put the PHP code to be executed 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 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 the execution time of method one and method two is similar, the running time of method four is the shortest, and the running time of method three is slightly longer.

Reference: https://cloud.tencent.com/developer/article/1053786 PHP generates 4 random passwords and performance comparison-Cloud + Community-Tencent Cloud