Skip to content

Instantly share code, notes, and snippets.

@oleganza
Created April 11, 2014 11:00
Show Gist options
  • Save oleganza/10458460 to your computer and use it in GitHub Desktop.
Save oleganza/10458460 to your computer and use it in GitHub Desktop.
js password generator
// Based on oi.js by Dan Kaminsky https://gist.github.com/PaulCapestany/6148566
var base58alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
function millis() { return Date.now(); }
function flip_coin() { n=0; then = millis()+1; while(millis()<=then) { n=!n; } return n; }
function get_fair_bit() { while(1) { a=flip_coin(); if(a!=flip_coin()) { return(a); } } }
function get_random_6bit(){ n=0; bits=6; while(bits--){ n<<=1; n|=get_fair_bit(); } return n; }
function get_password(length) {
var password = "";
var offset = 0; // offset allows us to go through the infinite alphabet thus minimizing bias towards lower characters.
length = (length ? length : 10)
while (password.length < length)
{
offset = (offset + get_random_6bit()) % base58alphabet.length;
password += base58alphabet[offset];
}
return password;
}
console.log(get_password(12));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment