【问题标题】:Generate Random Password without these characters "l1o0"生成不带这些字符“l1o0”的随机密码
【发布时间】:2016-01-24 12:26:31
【问题描述】:

我需要生成带有条件的随机密码:

  • 除了“l1o0”
  • 之外的所有字符都是允许的
  • 长度为 8 到 12 个字符

我尝试过的代码:

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

如何避免这 4 个字符 => "l1o0" ?

原因:

  • 这 4 个字符有时会使用户感到困惑。

谢谢!

【问题讨论】:

    标签: php security random passwords


    【解决方案1】:

    请不要使用当前提供的任何其他答案来生成密码。无论如何,它们都不安全。

    • rand() -> 没有
    • mt_rand() -> 绝对不是

    我将从标题为 How to Securely Generate Random Strings and Integers in PHP 的博文中提取此解决方案。

    /**
     * Note: See https://paragonie.com/b/JvICXzh_jhLyt4y3 for an alternative implementation
     */
    function random_string($length = 26, $alphabet = 'abcdefghijklmnopqrstuvwxyz234567')
    {
        if ($length < 1) {
            throw new InvalidArgumentException('Length must be a positive integer');
        }
        $str = '';
        $alphamax = strlen($alphabet) - 1;
        if ($alphamax < 1) {
            throw new InvalidArgumentException('Invalid alphabet');
        }
        for ($i = 0; $i < $length; ++$i) {
            $str .= $alphabet[random_int(0, $alphamax)];
        }
        return $str;
    }
    

    用法:

    // Every ASCII alphanumeric except "loIO01":
    $alphabet = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $string = random_string(12, $alphabet);
    

    您可能没有random_int(),除非您在将来 PHP 7 发布时阅读此内容。对于我们这些活在当下的人,请使用random_compat。

    【讨论】:

      【解决方案2】:

      试试这个:

      function generateRandomPassword($length = 8) {
          $characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
          $charactersLength = strlen($characters);
          $randomPassword = '';
          for ($i = 0; $i < $length; $i++) {
              $randomPassword .= $characters[rand(0, $charactersLength - 1)];
          }
          return $randomPassword;
      }
      

      【讨论】:

      • 仍然得到i ...我需要避免这4个字符...l 1 o 0
      • 你可以自己删除它:P 不管怎样,更新了 :)
      • 这不够安全,无法用于密码。 rand() 特别弱。
      【解决方案3】:

      您无需更改代码。只需使用 str_replace 替换那些单词你可以试试这个解决方案:)。刚刚编辑了你的代码

      function generateRandomPassword($length = 8) {
          $characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
          $charactersLength = strlen($characters);
          $randomPassword = '';
          for ($i = 0; $i < $length; $i++) {
              $randomPassword .= $characters[rand(0, $charactersLength - 1)];
          }
          return str_replace(['l','1','o','0'], ['A','B','C','D'], $randomPassword);
      }
      

      【讨论】:

      • 这不仅不安全(切勿使用rand() 生成密码),而且还偏向于这四个字符。
      【解决方案4】:
      $string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
      $req_pword_len = 20;
      
      $char_count = 0;
      $password='';
      $chars=str_split($string);
      while ( $char_count < $req_pword_len ) {
          $char = mt_rand(0,61);
          $password .= (string) $chars[$char];
          $char_count++;
      }
      

      更改值

      • $string 就是你想要允许的字符
      • $req_pword_len 到所需的密码长度

      【讨论】:

        【解决方案5】:

        试试这个:

        function generateRandomPassword($length = 8) {
        
            $randomPassword = '';
            for ($i = 0; $i < $length; $i++) {
              while (true) {
                  //remove 0,1,I,O,l,o
                  while(in_array(($number = rand(65, 122)), array(48, 49, 73, 79, 108, 111)));         
                  if ($number <= 90 or $number >= 97) {
                      $randomPassword .= chr($number);                                      
                      break ;
                   }
             }
          }
            return $randomPassword;
        }
        
        echo generateRandomPassword();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-15
          • 2010-09-08
          • 1970-01-01
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          相关资源
          最近更新 更多