Skip to content

Instantly share code, notes, and snippets.

@ahultgren
Last active December 14, 2015 05:48
Show Gist options
  • Save ahultgren/5037567 to your computer and use it in GitHub Desktop.
Save ahultgren/5037567 to your computer and use it in GitHub Desktop.
Basic password generator. Larger feed base than most other generators (and thus higher entropy) and let's you choose which character composition to use. Quotes are for example confusing to put in a session secret on the server, so exclude chars.ambigious. The PRNG can definitely be improved, as could the character collection. Right now, all char…
var chars = {
numbers: "123456789",
letters: "qwertyupasdfghjkzxcvbnm",
upper: "qwertyupasdfghjkzxcvbnm".toUpperCase(),
special: "!#€%&/@\\(){}[]=?`´^¨*,.;:-_><§°",
foreign: "ñõìàéèëêãâîïÇÉçéûüåäöæø",
foreignUpper: "ñõìàéèëêãâîïÇÉçéûüåäöæø".toUpperCase(),
ambigious: "”\"'’“iloILO0 ",
insane: "¡¥¢‰¶≠¿•˝√‡˜ˆŒ∏»«ˇ⁄—·◊∑∆∫¯˘¬ºflØÆ⁄ˇ≥Ω鮆µüıœπ˙ß∂ƒ¸˛ªfi÷≈ç‹›‘‚…"
};
function generatePassword (length, collection) {
var result = "", i;
collection = collection.map(function (item) {
return chars[item];
}).join('');
for(i = length; i--;) {
result += collection[Math.floor(Math.random()*collection.length)];
}
return result;
}
// Usage example
console.log(generatePassword(30, ['numbers', 'letters', 'upper', 'special', 'foreign', 'foreignUpper', 'insane']));
@ahultgren
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment