4

I need a random sequence of bytes for making a password hash. In Ruby, this would look like:

 File.open("/dev/urandom").read(20).each_byte{|x| rand << sprintf("%02x",x)}

In Node.js, I can get a sequence of random bytes with:

 var randomSource = RandBytes.urandom.getInstance();
 var bytes = randomSource.getRandomBytesAsync(20);

But the problem is, how to convert these to a String?

Also, I need to have them wrapped in promisses. Would this work:

   get_rand()
   .then(function(bytes) {
     authToken = bytes;
   })

5 Answers 5

31

Try this:

new Buffer(bytes).toString('ascii');

More details here: http://nodejs.org/api/buffer.html

Sign up to request clarification or add additional context in comments.

3 Comments

I like your answer since you mention the place to learn about this... however, I get: <Buffer b8 92 17 4c f1 11 35 d2 04 2a 7b 94 03 5c 9c 1a d0 9d 6a 43> - why is this not something as "saDasdkn2"
hmm.. maybe it is the promise library I am using, I filed an issue here: github.com/petkaantonov/bluebird/issues/34
new Buffer(bytes) was deprecated, you can use new Buffer.from(bytes) now
4

You can just use crypto that comes with node:

var Promise = require("bluebird");
var crypto = Promise.promisifyAll(require("crypto"));

crypto.randomBytesAsync(20).then(function(bytes){
    console.log('random byte string:', bytes.toString("hex"));
});

Logs:

random byte string: 39efc98a75c87fd8d5172bbb1f291de1c6064849

1 Comment

This seems like the correct answer actually. Not sure of the benefit of using randbytes these days. It definitely doesn't work on windows. (not that anyone cares... ;) )
2

randbytes works asynchronously. If you want to combine it with promises, you need to use a Promises-lib as well. I'm using when as an example:

var when          = require('when');
var RandBytes     = require('randbytes');
var randomSource  = RandBytes.urandom.getInstance();

function get_rand() {
  var dfd = when.defer();
  randomSource.getRandomBytes(20, function(bytes) {
    dfd.resolve( bytes.toString('hex') ); // convert to hex string
  });
  return dfd.promise;
}

// example call:
get_rand().then(function(bytes) {
  console.log('random byte string:', bytes);
});

2 Comments

this answer is very good too, small detail, I am using the bluebird library, and maybe my problem is there, I referenced to your answer here: github.com/petkaantonov/bluebird/issues/34
I think @Esailija's answer is actually the correct one.
2

If you are using ES6 then its also easy

String.fromCharCode(...bytes)
Promise.resolve(String.fromCharCode(...bytes)) // Promise

or

String.fromCharCode.apply(null, bytes)
Promise.resolve(String.fromCharCode.apply(null, bytes)) // Promise

Comments

0

Do you want to conver it to ASCII? If not, this is my code (1 minute of work):

  var z;
 randomSource.getRandomBytes(20, function(){z=arguments[0]})
 z
<Buffer c8 64 03 d1 2d 27 7d 8e 8f 14 ec 48 e2 97 46 84 5a d7 c7 2f>
 String(z)
'�d\u0003�-\'}��\u0014�H��F�Z��/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.