如何使用这些要求生成随机密码?

时间:2013-05-16 12:37:02

标签: javascript

我能够在C#中执行此操作,但无法将其转换为Javascript。我发现这篇文章“Generate random password string with requirements in javascript”,但我无法根据我的要求进行自定义:

密码长度至少为8个字符,最多13个字符,并且必须至少包含以下每个字符串集合中的一个字符:

string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
string numbers = "0123456789";
string smallLetters = "abcdefghijklmnopqrstuvwxyz";
string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
你能帮帮忙吗? 非常感谢!

修改

这是我在C#中的代码。对不起,这有点长:

private string CreateRandomPassword(int passwordLength)
        {
            string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
            string numbers = "0123456789";
            string smallLetters = "abcdefghijklmnopqrstuvwxyz";
            string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string allowedChars = "";

            char[] chars = new char[passwordLength];
            string password = String.Empty;
            Random rd = new Random();

            int ctr = 0;
            int prop = 4;

            if(!_password.ContainsSpecialCharacters && !_password.ContainsNumbers && !_password.ContainsSmallLetters && !_password.ContainsCapitalLetters)
                return String.Empty;

            string sc = "";
            string num = "";
            string sl = "";
            string cl = "";

            if(_password.ContainsSpecialCharacters)
            {
                // Get a special character randomly
                rd = new Random();
                sc = specialCharacters[rd.Next(0, specialCharacters.Length)].ToString();
                allowedChars += specialCharacters;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsNumbers)
            {
                // Get a random number randomly
                rd = new Random();
                num = numbers[rd.Next(0, numbers.Length)].ToString();
                allowedChars += numbers;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsSmallLetters)
            {
                // Get a small letter randomly
                rd = new Random();
                sl = smallLetters[rd.Next(0, smallLetters.Length)].ToString();
                allowedChars += smallLetters;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsCapitalLetters)
            {
                // Get a capital letter randomly
                rd = new Random();
                cl = capitalLetters[rd.Next(0, capitalLetters.Length)].ToString();
                allowedChars += capitalLetters;

            }
            else
            {
                prop--;
            }

            for (; ctr < passwordLength - prop; ctr++)
                password += allowedChars[rd.Next(0, allowedChars.Length)];


            return password + sc + num + sl + cl;
        }

3 个答案:

答案 0 :(得分:0)

这是我想出的。

只需在JavaScript控制台中运行即可。

(function() {
    var passwd, classTypes, classCount, len, chars, classIndex, charIndex;
    do {
        passwd = "";
        classTypes = [];
        classCount = 0;
        len = Math.round(Math.random() * 5 + 8);
        chars = ["~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,", "0123456789",
            "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
        while (passwd.length < len) {
            classIndex = Math.round(Math.random() * 3);
            classTypes[classIndex] = true;
            charIndex = Math.round(Math.random()
                * (chars[classIndex].length - 1));
            passwd += chars[classIndex].charAt(charIndex);
        }
        console.log(JSON.stringify(classTypes));
        classCount = classTypes.filter(function(value, index, object) {
            return value === true;
        }).length;
        console.log("classCount = " + classCount);
        console.log("passwd.length = " + passwd.length);
    } while (classCount < 4);
    console.log("generated pasword is \n" + passwd + "\n");
    console.log("but see http://xkcd.com/936/ for a better approach to paswords");
})();

答案 1 :(得分:-1)

要检查它是否包含任一字符串中的字符,您可以使用Javascript Regular Expressions。只需根据链接将候选密码与这些字符匹配即可。你打算尝试四场比赛。字符串的长度,您可以检查其length属性。

答案 2 :(得分:-2)

尝试

var specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
var numbers = "0123456789";
var smallLetters = "abcdefghijklmnopqrstuvwxyz";
var capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var srcs = [specialCharacters, numbers, smallLetters, capitalLetters];

function getRandomChar(string){
    var pos = Math.floor(Math.random() * string.length);
    return string.charAt(pos);
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}
function generate(){
    var len = 8 + Math.floor(Math.random() * 5);

    var array = [];
    array.push(getRandomChar(specialCharacters));
    array.push(getRandomChar(numbers));
    array.push(getRandomChar(smallLetters));
    array.push(getRandomChar(capitalLetters));

    for(var i = 4; i < len; i++){
        var pos = Math.floor(Math.random() * srcs.length);
        array.push(getRandomChar(srcs[pos]));
    }
    return shuffle(array).join('')
}

for(var i = 0; i < 10; i++){
    console.log(generate())
}

演示:Fiddle