简体   繁体   English

基本的Javascript密码生成器

[英]Basic Javascript Password Generator

I'm trying to create a simple javascript password generator that spits out the number of characters the user puts in. So, lets say you enter the number 7. I want the program to then spit out 7 random characters from the possibilities variable. 我正在尝试创建一个简单的JavaScript密码生成器,以吐出用户输入的字符数。因此,可以说您输入了数字7。我希望程序然后从可能性变量中吐出7个随机字符。 Below is my code so far: 下面是我到目前为止的代码:

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

}

I'm a bit new to JavaScript and am kind of stuck here so any help is appreciated :) Thx in advance! 我对JavaScript有点陌生,有点卡在这里,所以不胜感激:)提前谢谢!

Here is a working demo. 这是一个工作示例。 This is uses an existing answer found here . 这是使用此处找到的现有答案的方法。 I have added minor changes to fit this question. 我添加了一些细微的改动来适应这个问题。

 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; } } 
 <input type="text" id="IdLength" oninput="makeid()" placeholder="Enter 0-9"/> <span id="results"></span> 

I hope this helps. 我希望这有帮助。 Happy coding! 编码愉快!

If you are looking for something basic then you should try the Jen library (works both on browsers and nodejs/io.js). 如果您正在寻找基本的东西,那么您应该尝试Jen库 (在浏览器和nodejs / io.js上均可使用)。

And actually it is not recommended to use Math.random() for crypto application (such as password generation) 实际上,不建议将Math.random()用于加密应用程序(例如密码生成)

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. 这是JavaScript代码加上HTML代码,因此您可以尝试一下。 Basically, you can give Length anything that is a string(I haven't make an error checking for that). 基本上,您可以给Length提供字符串形式的任何内容(我没有对此进行错误检查)。 If the length is equal to 'rand' then the generator will generate a random password with a length between 19 to 30 characters. 如果长度等于“ rand”,则生成器将生成长度在19到30个字符之间的随机密码。 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. 如果不能将长度转换为数字或小于13,则密码生成器将生成长度为13的密码。

For the variable "not" and the feature of "Not". 对于变量“ not”和功能“ 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). 生成密码时,生成器将为s,n,u,l(总共4个)的每个变量生成一个字符。 It will then generate the rest of it length randomly using the characters from variable a. 然后,它将使用变量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>

Memorable password generator 难忘的密码生成器

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

Result: 结果:

password(8);//kutekaku
password(11);//rahubabatun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM