1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-02 10:20:05 +02:00

Add alphanumeric option to generate token without non-alphanumeric chars

This commit is contained in:
Sandro Ariel Gómez Araya
2017-11-13 11:51:42 -03:00
parent 00fa92269e
commit a1174300a8

View File

@@ -9,7 +9,7 @@ function srs(options, cb) {
}
var length = options['length'] || 32;
var alphanumeric = options['alphanumeric'] || false;
// async path
if (cb) {
crypto.randomBytes(length, function(err, buf) {
@@ -26,7 +26,11 @@ function srs(options, cb) {
function _finish (buf) {
var string = buf.toString('base64');
string = string.replace(/\//g,'_').replace(/\+/g,'-');
if (alphanumeric === true){
string = string.replace(/[\W_]+/g,'');
}else{
string = string.replace(/\//g,'_').replace(/\+/g,'-');
}
return string.substr(0, length);
}