基本的Javascript密码生成器

时间:2015-03-10 18:04:01

标签: javascript

我正在尝试创建一个简单的javascript密码生成器,它会吐出用户输入的字符数。所以,让我们输入数字7.我希望程序然后从可能性中吐出7个随机字符变量。以下是我目前的代码:

var possibilities = ['A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L',
  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  'y', 'z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
];

var special_chars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?"; //no special     characters

function generator() {

  if (document.getElementById("input").value == '' || document.getElementById("input").value > 61) {

    alert("Enter under 61 characters");

  } else if (isNaN(document.getElementById("input").value) || document.getElementById("input").value == special_chars) {

    alert("Enter numbers only");
  } else {
    //var userInput = document.getElementById("input").value -- get value of this when clicked, spit out number of items from this array based on the value collected

    for (var x = 0; x < possibilities.length; x++) {

      var content = possibilities[Math.floor(Math.random() * possibilities.length)];
    }

    document.getElementById("random_password").innerHTML = content;
  }

}

我对JavaScript有点新意,并且有点卡在这里所以任何帮助都很感激:)请提前预测!

4 个答案:

答案 0 :(得分:1)

这是一个有效的演示。这是使用找到的现有答案here。我添加了一些小改动以适应这个问题。

&#13;
&#13;
function makeid(){
var IdLength=document.getElementById('IdLength');
//Strip anything but 0 to 9
var UserInput=IdLength.value.replace(/[^0-9.]/g, "");
//Update input value
IdLength.value=UserInput;
var Results=document.getElementById('results');
var text = "";
var shuffle = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//Is input is empty?
if(IdLength!==''){
	for( var i=0; i < IdLength.value; i++ ){
    	text += shuffle.charAt(Math.floor(Math.random() * shuffle.length));
    }
    Results.innerHTML=text;
}
}
&#13;
<input type="text" id="IdLength" oninput="makeid()" placeholder="Enter 0-9"/>
<span id="results"></span>
&#13;
&#13;
&#13;

我希望这会有所帮助。快乐的编码!

答案 1 :(得分:0)

如果您正在寻找基本的东西,那么您应该尝试Jen library(适用于浏览器和nodejs / io.js)。

实际上不建议将Math.random()用于加密应用程序(例如密码生成)

答案 2 :(得分:0)

This is a small script that I wrote. I do use this on a day to day basis to generate passwords. It is not perfect but it will do the small tasks. Feel free to play around with it and make it more perfect.

This is the JavaScript code plus the HTML code so you can try it. Basically, you can give Length anything that is a string(I haven't make an error checking for that). If the length is equal to 'rand' then the generator will generate a random password with a length between 19 to 30 characters. If length can't be converted to a number or is lower than 13 then the password generator will generate a password with a length of 13.

For the variable "not" and the feature of "Not". The password generator will not generate a password that contained any character that you placed in the Not field.

When it comes to generating the password, the generator will generate one character for each variable of s, n, u, l(4 in total). It will then generate the rest of it length randomly using the characters from variable a. After that, the generator will shuffle the string that it just generated randomly. It will then alert you the length of the password and give the output of the password in the shadowed box.

Also, it is just easier to obtain a random character at a random position in the string then taking a character from a random index in an array. It is easier when it comes to reconfiguring the script.

<html>
<head>
<script type="text/javascript">
function rand( len, not ){
    if ( len === undefined ) len = 13;
    if ( typeof not !== 'string' ) not = '';
    if ( len === 'rand' ) len = Math.floor(Math.random() * (30 - 19) + 19);
    if ( typeof len === 'string' ) len = parseInt(len, 10);
    if ( len < 13 || isNaN(len)  ) len = 13;

    var s = '!@~!@#$%^&*()-_=+',
        n = '0123456789',
        u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        l = 'abcdefghijklmnopqrstuvwxyz',
        a = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@~!@#$%^&*()-_=+',
      out = '';

    if ( (nl = not.length) > 0 ){
        var regex = '';
        for ( var i = 0; i < nl; i++ ){
            regex = not.charAt(i).replace(/[.*+?^${}()|[\]\\]/, '\\$&');
            regex = new RegExp( regex , 'g');
            if ( s.length > 0 ) s = s.replace( regex, '');
            if ( n.length > 0 ) n = n.replace( regex, '');
            if ( u.length > 0 ) u = u.replace( regex, '');
            if ( l.length > 0 ) l = l.replace( regex, '');
            a = a.replace( regex, '');
        }
    }

    if ( s.length > 0 ) out += s.charAt( Math.floor(Math.random() * s.length) );
    if ( n.length > 0 ) out += n.charAt( Math.floor(Math.random() * n.length) );
    if ( u.length > 0 ) out += u.charAt( Math.floor(Math.random() * u.length) );
    if ( l.length > 0 ) out += l.charAt( Math.floor(Math.random() * l.length) );

    for ( var i = out.length; i < len; i++) out += a.charAt( Math.floor(Math.random() * a.length) );


    out = out.split('').sort(function(a, b){return 0.5 - Math.random()}).join('');
    alert(out.length);
    return out;
}

function generate(){
    document.getElementById("result").value = rand( document.getElementById("length").value, document.getElementById("not").value );
}

</script></head>
<body>

<label for="length">Length:</label><input type="text" id="length" maxlength="5" size="5">
<label for="not">Not:</label><input type="text" id="not" size="12">
<button onclick="generate();" type="button">Generate</button><br />

<input type="text" id="result" size="35" value="Result" disabled><br />


</body>
</html>

答案 3 :(得分:0)

难忘的密码生成器

// Also you can use a custom function to get random numbers instead
const _ = require('lodash/number');

/*
 * Generate comfortable to remember password
 *
 * @param integer length - length of generated password
 *
 * @return string password
 */
function my_create_password(length)
{
    let vowel = [
        'a',
        'e',
        'i',
        'o',
        'u',
    ];

    let consonant = [
        'b',
        'c',
        'd',
        'f',
        'g',
        'h',
        'j',
        'k',
        'l',
        'm',
        'n',
        'p',
        'q',
        'r',
        's',
        't',
        'v',
        'w',
        'x',
        'y',
        'z',
    ];

    result = '';
    for (let i = 0; i < length; i++) {
        if (i % 2 === 0) {
            result += consonant[_.random(0, 15)];
        } else {
            result += vowel[_.random(0, 4)];
        }
    }

    return result;
}

结果:

password(8);//kutekaku
password(11);//rahubabatun
相关问题