生成符合要求的随机密码

huangapple go评论46阅读模式
英文:

Generating random password with requirements

问题

以下是代码的翻译部分:

function generatePassword($_len) {
    
    $_alphaSmall = 'abcdefghijklmnopqrstuvwxyz';             // 小写字母
    $_alphaCaps  = strtoupper($_alphaSmall);                // 大写字母
    $_numerics   = '1234567890';                            // 数字
    $_specialChars = '%-#_*@';                              // 特殊字符
    
    $_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars;   // 包含所有字符
    $password = '';         // 将包含所需密码 -稍后使用
    
    for($i = 0; $i < $_len; $i++) {                                 // 循环直到达到指定长度
        $_rand = rand(0, strlen($_container) - 1);                  // 获取随机长度
        $password .= substr($_container, $_rand, 1);                // 返回字符串的一部分
    }
    
    return $password;       // 返回生成的密码
}

如果您需要确保生成的密码始终包含至少1个特殊字符和1个数字,您可以在循环之前添加一些逻辑来确保这一点。如果需要我添加此逻辑,请告诉我。

英文:

I am using the code below to generate a password, but I need it to meet the requirement of always having at least 1 special character and 1 number. How can I do this?

function generatePassword($_len) {

  $_alphaSmall = &#39;abcdefghijklmnopqrstuvwxyz&#39;;            // small letters
  $_alphaCaps  = strtoupper($_alphaSmall);                // CAPITAL LETTERS
  $_numerics   = &#39;1234567890&#39;;                            // numerics
  $_specialChars = &#39;%-#_*@&#39;;                              // Special Characters

  $_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars;   // Contains all characters
  $password = &#39;&#39;;         // will contain the desired pass - will use later

  for($i = 0; $i &lt; $_len; $i++) {                                 // Loop till the length mentioned
      $_rand = rand(0, strlen($_container) - 1);                  // Get Randomized Length
      $password .= substr($_container, $_rand, 1);                // returns part of the string 
  }

  return $password;       // Test Returns the generated Pass
}

答案1

得分: 1

以下是已翻译的代码部分:

一个重要的问题是,您是否希望字符串包含数字和特殊字符以确保安全性(这在使用此算法时没有意义,因为它会增加密码的可猜测性,而不是减少),还是为了满足特定网站或其他服务的要求。

在后一种情况下,最简单的方法可能是在字符串末尾添加数字和特殊字符。在前一种情况下,您可以将它们添加到末尾,然后对字符串进行混洗,以防止它们的位置可预测。

function generatePassword($_len) {

  $_alphaSmall = 'abcdefghijklmnopqrstuvwxyz';            // 小写字母
  $_alphaCaps  = strtoupper($_alphaSmall);                // 大写字母
  $_numerics   = '1234567890';                            // 数字
  $_specialChars = '%-#_*@';                              // 特殊字符

  $_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars;   // 包含所有字符
  $password = '';         // 将包含所需的密码 - 稍后将使用

  for($i = 2; $i < $_len; $i++) {                                 // 循环直到长度少两个字符
      $_rand = rand(0, strlen($_container) - 1);                  // 获取随机长度
      $password .= substr($_container, $_rand, 1);                // 返回字符串的一部分
  }
  $password .= substr($_numerics, rand(0, strlen($_numerics) - 1), 1);    // 添加一个数字
  $password .= substr($_specialChars, rand(0, strlen($_specialChars) - 1), 1);    // 添加一个特殊字符

    // 对密码字符进行混洗,因为数字和特殊字符位于固定位置
    $passwordArray = str_split($password);
    shuffle($passwordArray);
    $password = implode('', $passwordArray);

  return $password;       // 测试返回生成的密码
}
英文:

An important question here is whether you want your string to contain a number and special character for actual security reasons (which doesn't make sense with this algorithm, as it would increase, not decrease, the guess-ability of the password), or to satisfy the requirements of specific websites or other services.

In the latter case, the simplest method is probably to add a number and special character to the end of the string. In the former, you can add them to the end and then shuffle the string, to keep their position from being predictable.

 function generatePassword($_len) {

  $_alphaSmall = &#39;abcdefghijklmnopqrstuvwxyz&#39;;            // small letters
  $_alphaCaps  = strtoupper($_alphaSmall);                // CAPITAL LETTERS
  $_numerics   = &#39;1234567890&#39;;                            // numerics
  $_specialChars = &#39;%-#_*@&#39;;                              // Special Characters

  $_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars;   // Contains all characters
  $password = &#39;&#39;;         // will contain the desired pass - will use later

  for($i = 2; $i &lt; $_len; $i++) {                                 // Loop till two characters less than the desired length
      $_rand = rand(0, strlen($_container) - 1);                  // Get Randomized Length
      $password .= substr($_container, $_rand, 1);                // returns part of the string 
  }
  $password .= substr($_numerics, rand(0, strlen($_numerics) - 1), 1);    // Add a number
  $password .= substr($_specialChars, rand(0, strlen($_specialChars) - 1), 1);    // Add a special character

    // Shuffle the password characters because the number and the special characters comes at fixed position
    $passwordArray = str_split($password);
    shuffle($passwordArray);
    $password = implode(&#39;&#39;, $passwordArray);

  return $password;       // Test Returns the generated Pass
}

huangapple
  • 本文由 发表于 2023年6月1日 01:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定