Comparison of four methods and performance for generating random passwords in PHP

Source: Internet
Author: User
: This article mainly introduces four methods for generating random passwords in PHP and compares their performance. For more information about PHP tutorials, see. When using PHP to develop applications, especially website programs, you often need to generate random passwords. for example, if you register a user to generate a random password, you also need to generate a random password to reset the password. The random password is a string of fixed length. here I have collected several methods to generate random strings for your reference.

Method 1:

1. Generate a random integer in 33-126, for example, 35,

2. convert 35 to corresponding ASCII characters, for example, 35 #

3. Repeat steps 1 and 2 n and connect them to a n-bit password.

This algorithm mainly uses two functions. the mt_rand (int $ min, int $ max) function is used to generate random integers, where $ min-$ max is the ASCII code range, here, the range is 33-126. you can adjust the range as needed. for example, 97-122 characters in the ASCII code table correspond to English letters a-z. for details, refer to the ASCII code table. chr (int $ ascii) the function is used to convert the corresponding integer $ ascii to the corresponding character.

View sourceprint?

 
 
  1. Function create_password ($ pw_length = 8)
  2. {
  3. $ Randpwd = '';
  4. For ($ I = 0; $ I <$ pw_length; $ I ++)
  5. {
  6. $ Randpwd. = chr (mt_rand (33,126 ));
  7. }
  8. Return $ randpwd;
  9. }
  10. // Call this function to pass the length parameter $ pw_length = 6
  11. Echo create_password (6 );

Method 2:

1. preset a string $ chars, including A-z, a-Z, 0-9, and some special characters

2. random character in $ chars string

3. Repeat step 2 n times to obtain a password with a length of n.

View sourceprint?

 
 
  1. Function generate_password ($ length = 8 ){
  2. // Password character set, which can be any character you need
  3. $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
  4. $ Password = '';
  5. For ($ I = 0; $ I <$ length; $ I ++)
  6. {
  7. // Two character acquisition methods are provided here
  8. // Use substr to intercept any character in $ chars;
  9. // The second method is to take any element of the character array $ chars.
  10. // $ Password. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1 );
  11. $ Password. = $ chars [mt_rand (0, strlen ($ chars)-1)];
  12. }
  13. Return $ password;
  14. }

Method 3:

1. preset a character array $ chars, including A-z, a-Z, 0-9, and some special characters

2. use array_rand () to randomly select $ length elements from the array $ chars.

3. extract the string from the array $ chars based on the obtained key name array $ keys. The disadvantage of this method is that the same characters are not repeated.

View sourceprint?

 
 
  1. Function make_password ($ length = 8)
  2. {
  3. // Password character set, which can be any character you need
  4. $ Chars = array ('A', 'B', 'C', 'D', 'e', 'F', 'G', 'H ',
  5. 'I', 'J', 'K', 'L', 'M', 'n', 'O', 'P', 'Q', 'R ','s ',
  6. 'T', 'u', 'V', 'W', 'X', 'y', 'z', 'A', 'B', 'C ', 'D ',
  7. 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'n ', 'o ',
  8. 'P', 'Q', 'R','s ', 'T', 'u', 'V', 'W', 'X', 'y ', 'Z ',
  9. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9 ', '! ',
  10. '@', '#', '$', '%', '^ ','&','*','(',')','-', '_',
  11. '[', ']', '{', '}', '<', '> ','~ ', ''',' + ',' = ',',',
  12. '.',';',':','/','? ',' | ');
  13. // Random $ length array element key names in $ chars
  14. $ Keys = array_rand ($ chars, $ length );
  15. $ Password = '';
  16. For ($ I = 0; $ I <$ length; $ I ++)
  17. {
  18. // Concatenate $ length array elements into strings
  19. $ Password. = $ chars [$ keys [$ I];
  20. }
  21. Return $ password;
  22. }

Method 4:

This method is a new method provided by a netizen after being reproduced by the blue ideal in this article. the algorithm is simple and the code is short, just because of the return value of the md5 () function, the generated password only contains letters and numbers, but it is also a good method. Algorithm idea:

1. time () gets the current Unix timestamp

2. encrypt the timestamp obtained in step 1 with md5 ()

3. extract the data encrypted in step 2 to obtain the desired password.

View sourceprint?

 
 
  1. function get_password( $length = 8 )
  2. {
  3. $str = substr(md5(time()), 0, 6);
  4. return $str;
  5. }

Time efficiency comparison

We use the following PHP code to calculate the running time of the above four random password generation functions to generate six passwords, and then compare their time efficiency.

View sourceprint?

 
 
  1. Function getmicrotime ()
  2. {
  3. List ($ usec, $ sec) = explode ("", microtime ());
  4. Return (float) $ usec + (float) $ sec );
  5. }
  6. // Record start time
  7. $ Time_start = getmicrotime ();
  8. // Put the PHP code to be executed here, for example:
  9. // Echo create_password (6 );
  10. // Record end time
  11. $ Time_end = getmicrotime ();
  12. $ Time = $ time_end-$ time_start;
  13. // Output the total running time
  14. Echo "execution time $ time seconds ";
  15. ?>

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

We can see that method 1 and method 2 have almost the same execution time, and method 4 has the shortest time, while Method 3 has a longer running time.

The above describes four methods for generating random passwords in PHP and their performance comparison, including some content. I hope my friends who are interested in the PHP Tutorial will be helpful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.