改进安全密码生成器

时间:2016-12-21 17:03:51

标签: javascript jquery

简单密码生成器示例:

function randomPassword() {
    var chars = "abcdefghijklmnopqrstuvwxyz" +
    			"ABCDEFGHIJKLMNOP" + 
                "1234567890" +
                "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/",
    	pass  = "",
        PL    = 10;
    
    for (var x = 0; x < PL; x++) {
        var i = Math.floor(Math.random() * chars.length);
        pass += chars.charAt(i);
    }
    return pass;
}

function generate() {
    myform.row_password.value = randomPassword();
}
<form name="myform" method="post" action="">
    <table width="100%" border="0">
        <tr>
            <td>Password:</td>
            <td>
                <input name="row_password" type="text" size="40">&nbsp;
                <input type="button" class="button" value="Generate" onClick="generate();" tabindex="2">
            </td>
        </tr>
    </table>
</form>

改进功能问题

1)。获取变量中的所有值

根据上面的基本脚本,如何调用chars.lengthchars.charAt(i) chars等于Chars内的所有值?

var Chars = {};
    Chars.abc   = "abcdefghijklmnopqrstuvwxyz";
    Chars.ABE   = "ABCDEFGHIJKLMNOP";
    Chars.Num   = "1234567890";
    Chars.Sym   = "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/";

2)。为不太高级的密码实施复选框系统

要生成不太高级的密码,例如通过取消选中复选框不包括symbox,我该如何设置密码才能使用Chars.abcChars.ABEChars.Num值?< / p>

第3)。同等地将密码长度除以Chars

向下舍入(密码长度/ Chars使用),即;此问题中使用的示例生成一个10个字符的密码并使用所有字符,因此每个Chars至少有2个。

1 个答案:

答案 0 :(得分:1)

缺少第三个功能,可能会更复杂。但这是第1和第2个的简单解决方案。

var output = document.getElementsByTagName('output')[0];

var Chars = {};
    Chars.length = 16;
    Chars.abc   = "abcdefghijklmnopqrstuvwxyz";
    Chars.ABE   = "ABCDEFGHIJKLMNOP";
    Chars.Num   = "1234567890";
    Chars.NumRequired = true;
    Chars.Sym   = "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/";

var generator = new randomPasswordGenerator(Chars);

var simpleGenerator = new randomPasswordGenerator({
    length: 6,
    abc: 'abc',
    Num: '0'
});

var button = document.getElementsByTagName('button')[0];
    button.addEventListener('click', clickFunction);

var checkbox = document.getElementsByTagName('input')[0];

function clickFunction () {
  if (checkbox.checked) output.textContent = simpleGenerator.randomPassword();
  else output.textContent = generator.randomPassword();
}

function randomPasswordGenerator(opts) {
    for(var p in opts) this[p] = opts[p];
    this.randomPassword = randomPassword;
}

function randomPassword() {
    var chars = (this.abc || "") +
                (this.ABE || "") + 
                (this.Num || "") +
                (this.Sym || ""),
    	pass  = [],
        PL    = this.length;
    if (this.NumRequired) {
        var r = Math.floor(Math.random() * this.Num.length);
        var i = Math.floor(Math.random() * PL);
        pass[i] = this.Num[r];
    }
    for (var x = 0; x < PL; x++) {
        if(!pass[x]) {
          var i = Math.floor(Math.random() * chars.length);
          pass[x] = chars.charAt(i);
        }
    }
    return pass.join('');
}
output {
  margin: 12px;
  display: block;
  border-bottom: 1px solid
}
<button>Generate</button>
  <input type="checkbox">Simple
  <output></output>