1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-02 18:30:04 +02:00

Add support for 'sync' API. Updates code, tests and docs.

This commit is contained in:
Mark Stosberg
2015-08-06 12:06:40 -04:00
parent 350fd45086
commit a1cab50548
4 changed files with 52 additions and 21 deletions

View File

@@ -9,11 +9,17 @@ function srs(options, cb) {
}
var length = options['length'] || 32;
crypto.randomBytes(length, function(ex, buf) {
if (ex) throw ex;
cb(_finish(buf));
});
// async path
if (cb) {
crypto.randomBytes(length, function(ex, buf) {
if (ex) throw ex;
return cb(_finish(buf));
});
}
// sync path
else {
return _finish(crypto.randomBytes(length));
}
function _finish (buf) {
var string = buf.toString('base64');