[英]Creating a strong password with javascript
我有一個如下功能,它創建一個強密碼,但它並不總是添加一個特殊字符和數字或大寫字母,我如何確保它始終包含這些要求。
密碼要求是必須的
長度為8個或更多字符,必須包含特殊字符,大寫字母和數字。
我現在擁有的功能如下所示
function GenPwd(nMinimumLength,bIsRequired,sNameoftheButton){
var end=0;
var index=0;
var sPassCharactersWithoutSpeacil="!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
var sSpecialCharacters="!#$@*";
var nSrngLen=sStrgStrAddOnChrs
var sNumbers="123456789";
var nMinimunLen=Math.round(nBaseLen/2);
var PassWordDLength=Math.round((Math.random()*nMinLen)+nMinimumLength+nMinimumLength); //Make sure it is atleadt 8 characters long
var Password="";
var PasswordTemporary="";
for(index = 1; index <= PassWordDLength; index++) {
nCharacter = Math.round(Math.random()*65);
PasswordTemporary = sPassCharactersWithoutSpeacil.charAt(nCharacter);
while (PWD.indexOf(PasswordTemporary) > -1) {
nCharacter = '';
nCharacter = Math.round(Math.random()*65);
PasswordTemporary = sPassCharactersWithoutSpeacil.charAt(nChar);
}
FinalPWD = Password + PasswordTemporary;
}
這是我必須使用的
function GenPwd(nBaseLen,bIsStrong,sBtnName){
var end=0;
var index=0;
var sPassStr="!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
var sStrgStrAddOnChrs="!#$@*";
var nSrngLen=sStrgStrAddOnChrs
var sStrgStrAddOnNum="123456789";
var nMinLen=Math.round(nBaseLen/2);
var PWDLen=Math.round((Math.random()*nMinLen)+nBaseLen);
var PWD="";
var PWDTmp="";
//Generate the password
for(index = 1; index <= PWDLen; index++) {
nChar = Math.round(Math.random()*65);
PWDTmp = sPassStr.charAt(nChar);
while (PWD.indexOf(PWDTmp) > -1) {
nChar = '';
nChar = Math.round(Math.random()*65);
PWDTmp = sPassStr.charAt(nChar);
}
PWD = PWD + PWDTmp;
}
document.getElementById("pwd").value=PWD;
EnableBtn(sBtnName);
return true;
}
您可以創建一個函數來確保您的pwd至少包含一組特定的字符:
function ensurePwdContains(pwd, regexListOfChars, listOfChars) {
if (!regexListOfChars.test(pwd)) {
// select random char from listOfChars
var newChar = listOfChars.charAt(Math.floor(Math.random() * listOfChars.length));
// insert it into the current pwd in a random location
// must do +1 to include the location at the end of the string
var pos = Math.floor(Math.random() * (pwd.length + 1));
pwd = pwd.slice(0, pos) + newChar + pwd.slice(pos);
}
return pwd;
}
此函數將測試密碼是否包含至少一個傳入的正則表達式實例。如果沒有,它將從listOfChars
參數中選擇一個隨機字符,然后將其插入到隨機位置的現有密碼中。
然后,在函數結束時為兩種情況中的每一種調用此函數:
FinalPwd = ensurePwdContains(FinalPwd, /[!#\$@*]/, sSpecialCharacters);
FinalPwd = ensurePwdContains(FinalPwd, /[A-Z]/, "ABCDEFGHIJKLMNOPQRSTUWXYZ");
演示: http : //jsfiddle.net/jfriend00/ur9FL/
而且,這是您整體實施的清理版本。 您指的是函數中的一些變量,這些變量未在您的帖子中定義或未在您的函數中使用,因此我刪除了這些變量(因為我不知道它們是什么或它們是如何使用的)。 這是一個更清潔的實現:
function generatePwd(minLen) {
// create a pwd that is between minLen and ((2 * minLen) + 2) long
// don't repeat any characters
// require at least one special char and one capital char
function rand(max) {
return Math.floor(Math.random() * max);
}
function ensurePwdContains(pwd, regexListOfChars, listOfChars) {
if (!regexListOfChars.test(pwd)) {
var newChar = listOfChars.charAt(rand(listOfChars.length));
var pos = rand(pwd.length + 1);
pwd = pwd.slice(0, pos) + newChar + pwd.slice(pos);
}
return pwd;
}
var legalChars = "!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
var specialChars = "!#$@*";
var specialRegex = /[!#\$@*]/;
var caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var capsRegex = /[A-Z]/;
var nums = "123456789";
var numRegex = /[1-9]/;
// make legalChars into an array of chars (easier to deal with)
legalChars = legalChars.split("");
var pwd = "", len = minLen + rand(minLen), index;
// ensure len is not too long
len = Math.min(legalChars.length, len);
// now add chars to the pwd string until it is of the desired length
while (pwd.length < len) {
index = rand(legalChars.length);
pwd += legalChars[index];
// remove the char we just used
legalChars.splice(index, 1);
}
// ensure we have at least one special character and one caps char and one 1-9
pwd = ensurePwdContains(pwd, specialRegex, specialChars);
pwd = ensurePwdContains(pwd, capsRegex, caps);
pwd = ensurePwdContains(pwd, numRegex, nums);
return pwd;
}
並且,一個生成一大堆密碼的工作演示,以便您可以看到您獲得的變體類型: http : //jsfiddle.net/jfriend00/7dReY/
為了把密碼放到位,我建議你做第二個函數,調用第一個函數生成密碼並將其放入選擇的字段:
function putPasswordInPlace(pwdId, sBtnName) {
var pwd = generatePwd(8);
document.getElementById(pwdId).value = pwd;
EnableButton(sBtnName);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.