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:
@@ -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;
|
||||||
|
Reference in New Issue
Block a user