Generate random random string in NodeJS

3

Because, using the Javascript language on the NodeJS platform, generating a string, preferably with configurable size, random enough to be used in cryptographic routines can not be insufficiently random?

If you propose more than one solution, preferably specify which one you think is most random, possibly with additional external references . Solutions that involve more than one source of randomness are also welcome, especially if accompanied by an explanation of advantages, such as reducing problems with an operating system explicitly altered by a NSA of life to generate less chaos when it comes to producing randomness .

Even if two people propose the same code, if one of these explains better or proves with real examples the dispersion of values, it will be considered as the best answer. Do not just copy and paste code, you should give it a try because it's a good solution.

Solutions that directly access an operating system feature, for example using exec = require('child_process').exec; //(...) are also welcome.

Note: Please do not propose solutions that require access to an external service via the network. The solution should use only the language and operating system it is running on. Accessing via HTTP any service outside an internal network not only impacts performance but is also a cause of security breach.

    
asked by anonymous 15.02.2014 / 20:28

1 answer

3

Use the Crypto module:

var crypto = require('crypto');
crypto.randomBytes(tamanho, function (erro, resultado /*buffer*/) {
  if (erro) {throw erro;}
  console.log('Uma string aleatória de %d bytes: %s', resultado .length, resultado);
});
    
15.02.2014 / 21:04