[英]Password generator, how to make it return strong enough passwords
I created a simple password generator that is also strength check capable. 我创建了一个简单的密码生成器,它也可以进行强度检查。 However, I struggle to decide how it should behave if it fails to generate a strong enough password.
但是,如果它无法生成足够强的密码,我将很难决定如何处理。 At this point, it just displays a creation failure message.
此时,它仅显示创建失败消息。
Should I define the password creation rules or just make the function re-launch if the password created is not strong enough? 如果创建的密码强度不够,我应该定义密码创建规则还是只是重新启动该功能? Any remarks, suggestions or leads are appreciated.
任何意见,建议或线索,不胜感激。 Thanks.
谢谢。
The code can be found below. 该代码可以在下面找到。
'use strict';
let generatePassword = function(passwordLength = 10) {
let alphabetString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-_=+[]{}!@#$%^&*();:'|\,<.>/?`",
alphabet = alphabetString.split("",),
passwordArray = [],
getRandomSymbol = function() {
return alphabet[Math.floor(Math.random()*alphabet.length)]
};
for (let i = 0; i < passwordLength; i++) {
passwordArray.push(getRandomSymbol());
}
let password = passwordArray.join(""),
passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
if (password.match(passwordStrengthCheck)) {
return console.log(`Your password is: ${password}`);
} else {
console.log("Failed to generate a strong enough password");
}
}
// Function call sample:
generatePassword();
To ensure that your password has at least one symbol of all groups, do like that: 为确保您的密码在所有组中至少包含一个符号,请执行以下操作:
all
all
挑选剩下的字符 code: 码:
let generatePassword = function(passwordLength = 10) {
let capitals = "QWERTYUIOPASDFGHJKLZXCVBNM";
let small = "qwertyuiopasdfghjklzxcvbnm";
let digits = "1234567890";
let symbols = "-_=+[]{}!@#$%^&*();:'|\,<.>/?`",
let all = capitals + small + digits + symbols;
getRandomSymbol = function(sourceString) {
return sourceString[Math.floor(Math.random()*sourceString.length)]
};
alphabet = alphabetString.split("",),
passwordArray = [],
passwordArray.push(getRandomSymbol(capitals));
passwordArray.push(getRandomSymbol(small));
passwordArray.push(getRandomSymbol(digits));
passwordArray.push(getRandomSymbol(symbols));
for (let i = 0; i < passwordLength - 4; i++) {
passwordArray.push(getRandomSymbol(all));
}
shuffle = function (a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
let password = shuffle(passwordArray).join(""),
passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
if (password.match(passwordStrengthCheck)) {
return console.log(`Your password is: ${password}`);
} else {
console.log("Failed to generate a strong enough password");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.