Helma logo
helma.org » Home > docs > examples > Random Password Generator

Random Password Generator

This might be useful for suggesting passwords to the user, or if users are created automatically.

function getRandomPassword(digits) {
 var base = setaz09();
 var result = new Array();
 for (var i=0; i<digits; i++) {
  result[i] = base[Math.floor(Math.random()*base.length)]
 }
 return result.join("");
}

function setaz09() {
 var result = new Array();
 var i = 0;
 var tmp = "0";
 var code = tmp.charCodeAt(0);
 while (true) {
  result[i] = String.fromCharCode(code);
  i++;
  if (String.fromCharCode(code)=="9") {
   tmp = "a";
   code = tmp.charCodeAt(0);
  } else if (String.fromCharCode(code)=="z") {
   break
  } else {
   code++
  }
 }
 return result;
}
global/password.js


Up: Code Snippets
Previous: Image Processing Next: Sparse XML Parsing

... comment


Page last modified on 2001-09-05 14:45 by hns

 
chris.boulter, Friday, 13. October 2006, 14:57
Bookmarklet for convenient running
You can run this script from a convenient bookmarklet in your browser - create a bookmark called say 'Generate random password' with the following URL. When you click it, a new random 8-char password will appear in an 'alert' box:

javascript:function getRandomPassword(digits) { var base = setaz09(); var result = new Array(); for (var i=0; i<digits; i++) { result[i] = base[Math.floor(Math.random()*base.length)] } return result.join(""); } function setaz09() { var result = new Array(); var i = 0; var tmp = "0"; var code = tmp.charCodeAt(0); while (true) { result[i] = String.fromCharCode(code); i++; if (String.fromCharCode(code)=="9") { tmp = "a"; code = tmp.charCodeAt(0); } else if (String.fromCharCode(code)=="z") { break } else { code++ } } return result; }; alert(getRandomPassword(8));

... link  


... comment