gpt4 book ai didi

javascript - 使用 javascript 创建强密码

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:03 25 4
gpt4 key购买 nike

我有一个如下所示的函数,它创建了一个强密码,但它并不总是添加一个特殊字符和一个数字或大写字母,我如何确保它始终包含这些要求。

密码要求是必须的

8 or more characters long and has to have a special character,a capital letter and a number included to it.

下面显示了我现在拥有的功能

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

最佳答案

您可以创建一个函数来确保您的密码至少包含一组特定字符中的一个:

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

关于javascript - 使用 javascript 创建强密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24960368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com