From a1174300a83b340f1b156962aefdbd54a8129b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Ariel=20Go=CC=81mez=20Araya?= Date: Mon, 13 Nov 2017 11:51:42 -0300 Subject: [PATCH] Add alphanumeric option to generate token without non-alphanumeric chars --- lib/secure-random-string.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/secure-random-string.js b/lib/secure-random-string.js index 972e5f9..e15dbdf 100644 --- a/lib/secure-random-string.js +++ b/lib/secure-random-string.js @@ -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); }