Advertisement
k98kurz

Simple Random Password Generator

Dec 15th, 2014
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Dependency: Fisher-Yates Array Shuffle
  2. Array.prototype.shuffle = function () {
  3.     var i, j, t;
  4.     for (i=this.length-1; i>0; --i) {
  5.         j = Math.floor(Math.random()*(i+1)); t = this[i]; this[i] = this[j]; this[j] = t;
  6.     }
  7.     return this;
  8. };
  9.  
  10. function RandomPasswordGenerator () {
  11.     var chars = "";
  12.     for (var i=32; i<127; chars+=String.fromCharCode(i), ++i);
  13.     chars = chars.split("");
  14.  
  15.     this.generate = function(nlength) {
  16.         var n = (typeof nlength == "number" ? nlength : 12);
  17.         var s = "";
  18.         while (s.length<n) {
  19.             chars.shuffle();
  20.             s += chars[Math.random()*chars.length|0];
  21.         }
  22.         return s;
  23.     };
  24.  
  25.     // returns array of random passwords
  26.     this.generatePasswords = function(npasswords, nlength) {
  27.         var l = (typeof nlength == "number" ? nlength : 12);
  28.         var n = (typeof npasswords == "number" ? npasswords : 10);
  29.         var p = []; for (var i=0; i<n; p.push(this.generate(l)), ++i);
  30.         return p;
  31.     };
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement