Created
March 3, 2014 23:56
-
-
Save skeggse/9337261 to your computer and use it in GitHub Desktop.
Generate crypto-random hex from command line because /dev/urandom isn't good enough for some reason
This file contains hidden or 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
#!/usr/bin/env node | |
var crypto = require('crypto'); | |
var chars = parseInt(process.argv[2], 10) || 32; | |
var blockSize = 1024 * 1024; | |
function sync(count) { | |
console.log(crypto.randomBytes(Math.ceil(count / 2)).toString('hex').slice(0, count)); | |
} | |
function more(remain) { | |
if (remain <= blockSize) | |
return sync(remain); | |
crypto.randomBytes(blockSize / 2, function(err, data) { | |
if (err) { | |
console.error(err.message); | |
return process.exit(1); | |
} | |
process.stdout.write(data.toString('hex')); | |
more(remain - blockSize); | |
}); | |
} | |
more(chars); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It does appear to be a little faster than the bash solution I tested (which included converting to hex).