How to generate random password with PHP?

后端 未结 19 2106
栀梦
栀梦 2020-12-01 00:45

Or is there a software to auto generate random passwords?

相关标签:
19条回答
  • 2020-12-01 01:23

    Memorable password generator

    function password($length)
      {
        $vowel = array(
            'a',
            'e',
            'i',
            'o',
            'u',
        );
    
        $consonant = array(
            'b',
            'c',
            'd',
            'f',
            'g',
            'h',
            'j',
            'k',
            'l',
            'm',
            'n',
            'p',
            'q',
            'r',
            's',
            't',
            'v',
            'w',
            'x',
            'y',
            'z',
        );
    
        $result = '';
        for ($i = 0; $i < $length; $i++) {
            if ($i % 2 == 0) {
                $result .= $consonant[rand(0, 15)];
            } else {
                $result .= $vowel[rand(0, 4)];
            }
        }
    
        return $result;
    }
    

    The result:

    password(8);//kutekaku
    password(11);//rahubabatul
    
    0 讨论(0)
  • 2020-12-01 01:24

    I use st_split and implode function:

    function genarateKey(){
        $limit= 8;
        $chars= 'abcdefghijklmnopqrstuvxwyz1234567890!@#$%^&*()_+=-[]{}\|ABCDEFGHIJKLMNOPQRSTUVXWYZ';
        $chararray= str_split($chars);
        $gen=array();
        for($i=0;$i<$limit;$i++){
            $index=rand(0,strlen($chars)-1);
            $gen[$i]= $chararray[$index];
        }
        return implode($gen); //converts array to string
    }
    
    0 讨论(0)
  • 2020-12-01 01:25

    Here's a simple solution. It will contain lowercase letters and numbers.

    substr(str_shuffle(strtolower(sha1(rand() . time() . "my salt string"))),0, $PASSWORD_LENGTH);
    

    Here's A stronger solution randomly generates the character codes in the desired character range for a random length within a desired range.

    function generateRandomPassword() {
      //Initialize the random password
      $password = '';
    
      //Initialize a random desired length
      $desired_length = rand(8, 12);
    
      for($length = 0; $length < $desired_length; $length++) {
        //Append a random ASCII character (including symbols)
        $password .= chr(rand(32, 126));
      }
    
      return $password;
    }
    
    0 讨论(0)
  • 2020-12-01 01:26

    I want to play the game. The simplest way would be to do:

    function rand_passwd( $length = 8, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ) {
        return substr( str_shuffle( $chars ), 0, $length );
    }
    

    This is pretty much just a modification of the first answer. Specify the characters you want in the second parameters and the length of the password in the first.

    0 讨论(0)
  • 2020-12-01 01:26

    This is how I do it.

    $pw = ""; 
    for ($i = 0; $i < 13; $i++)
    {
        $pw .= chr(rand(33, 126));
    }
    
    0 讨论(0)
  • 2020-12-01 01:26

    This function will generate more stronger password than most woted solution:

    function generatePassword($size=8){
        $p = openssl_random_pseudo_bytes(ceil($size*0.67), $crypto_strong);
        $p = str_replace('=', '', base64_encode($p));
        $p = strtr($p, '+/', '^*');
        return substr($p, 0, $size);      
    }
    

    Each character of password will be [A-Z] or [a-z] or ^ or *

    0 讨论(0)
提交回复
热议问题