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

Refactor: encapsulate code that will be shared with the sync version.

This commit is contained in:
Mark Stosberg
2015-08-06 11:36:19 -04:00
parent 327ba5fa09
commit 350fd45086

View File

@@ -12,15 +12,19 @@ function srs(options, cb) {
crypto.randomBytes(length, function(ex, buf) { crypto.randomBytes(length, function(ex, buf) {
if (ex) throw ex; if (ex) throw ex;
cb(_finish(buf));
});
function _finish (buf) {
var string = buf.toString('base64'); var string = buf.toString('base64');
if (options.urlsafe) { if (options.urlsafe) {
string = string.replace(/\//g,'_').replace(/\+/g,'-'); string = string.replace(/\//g,'_').replace(/\+/g,'-');
} }
return string.substr(0, length);
cb(string.substr(0, length)); }
});
}; };
module.exports = srs; module.exports = srs;