Skip to content

Instantly share code, notes, and snippets.

@ericraio
Created September 4, 2012 01:25
Show Gist options
  • Save ericraio/3615573 to your computer and use it in GitHub Desktop.
Save ericraio/3615573 to your computer and use it in GitHub Desktop.
// Secure random hex in js like SecureRandom.hex in ruby
function secureRandom(n){
n = n+1 || 17;
var str = [];
while (--n) {
var r = Math.floor(Math.random()*256)
.toString(16)
.substr(Math.floor(Math.random()*2),1)
|| String.fromCharCode(65+(Math.floor(Math.random()*8)));
str.push(r);
}
return str.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment