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