英文:
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 = 'abcdefghijklmnopqrstuvwxyz'; // small letters
$_alphaCaps = strtoupper($_alphaSmall); // CAPITAL LETTERS
$_numerics = '1234567890'; // numerics
$_specialChars = '%-#_*@'; // Special Characters
$_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars; // Contains all characters
$password = ''; // will contain the desired pass - will use later
for($i = 0; $i < $_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 = 'abcdefghijklmnopqrstuvwxyz'; // small letters
$_alphaCaps = strtoupper($_alphaSmall); // CAPITAL LETTERS
$_numerics = '1234567890'; // numerics
$_specialChars = '%-#_*@'; // Special Characters
$_container = $_alphaSmall.$_alphaCaps.$_numerics.$_specialChars; // Contains all characters
$password = ''; // will contain the desired pass - will use later
for($i = 2; $i < $_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('', $passwordArray);
return $password; // Test Returns the generated Pass
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论