Created
April 11, 2014 10:50
-
-
Save dchest/10457887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRandomBytes(length) { | |
var bytes = new Uint8Array(length); | |
window.crypto.getRandomValues(bytes); | |
return bytes; | |
} | |
function generatePassword(length) { | |
// To avoid modulo bias, alphabet length must conform to 256 % length == 0, e.g. 64. | |
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@'.split(''); | |
var bytes = getRandomBytes(length); | |
var result = "", i; | |
for (i = 0; i < length; i++) { | |
result += alphabet[bytes[i] % alphabet.length]; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment