Friday, December 20, 2013

Random Characters in NodeJS

Quick example on how to generate random characters in NodeJS
var crypto = require('crypto');
var randomChars = crypto.randomBytes(50).toString('base64');
That makes a synchronous call to randomBytes, so keep that in mind. If you want asynchronous then pass a second param to randomBytes:
var crypto = require('crypto');
crypto.randomBytes(70, function(err, buf){
 console.log(buf.toString('base64'));
});
more info can be found here