使用javascript创建一个强密码

时间:2014-07-25 16:31:52

标签: javascript

我有一个如下功能,它创建一个强密码,但它并不总是添加一个特殊字符和一个数字或大写字母,我如何确保它始终包含这些要求。< / p>

密码要求必须

  

长度为8个或更多字符且必须具有特殊字符,a   大写字母和包含在其中的数字。

我现在拥有的功能显示在

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

1 个答案:

答案 0 :(得分:3)

您可以创建一个函数来确保您的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);
}