k98kurz icon

Simple Random Password Generator

k98kurz | PRO | 12/16/14 03:55:55 AM UTC | 0 ⭐ | 714 👁️ | Never ⏰ | []
JavaScript |

900 B

|

None

|

0 👍

/

0 👎

// Dependency: Fisher-Yates Array Shuffle
Array.prototype.shuffle = function () {
    var i, j, t;
    for (i=this.length-1; i>0; --i) {
        j = Math.floor(Math.random()*(i+1)); t = this[i]; this[i] = this[j]; this[j] = t;
    }
    return this;
};
 
function RandomPasswordGenerator () {
    var chars = "";
    for (var i=32; i<127; chars+=String.fromCharCode(i), ++i);
    chars = chars.split("");
 
    this.generate = function(nlength) {
        var n = (typeof nlength == "number" ? nlength : 12);
        var s = "";
        while (s.length<n) {
            chars.shuffle();
            s += chars[Math.random()*chars.length|0];
        }
        return s;
    };
 
    // returns array of random passwords
    this.generatePasswords = function(npasswords, nlength) {
        var l = (typeof nlength == "number" ? nlength : 12);
        var n = (typeof npasswords == "number" ? npasswords : 10);
        var p = []; for (var i=0; i<n; p.push(this.generate(l)), ++i);
        return p;
    };
}

Comments