Skip to content

Instantly share code, notes, and snippets.

@amfg
Created February 7, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amfg/502d1eafcf60cb5a7b4d to your computer and use it in GitHub Desktop.
Save amfg/502d1eafcf60cb5a7b4d to your computer and use it in GitHub Desktop.
Generate random base62 string the right way using window.crypto
random = function(length) {
var base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');
var array = new Uint8Array(length);
window.crypto.getRandomValues(array);
var str = '';
for (var i = 0; i < array.length; i++) {
str += base[array[i]%base.length];
};
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment