diff --git a/lib/securerandomstring.js b/lib/securerandomstring.js index d0c8324..b1d84e5 100644 --- a/lib/securerandomstring.js +++ b/lib/securerandomstring.js @@ -13,6 +13,10 @@ function securerandomstring(options, cb) { if (ex) throw ex; var string = buf.toString('base64'); + if (options.urlsafe) { + string = string.replace(/\//g,'_').replace(/\+/g,'-'); + } + cb(string.substr(0, length)); }); diff --git a/package.json b/package.json index 0ed5752..1257e79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "securerandomstring", - "version": "0.0.2", + "version": "0.0.3", "description": "Generates a secure random string with a given length", "main": "lib/securerandomstring.js", "scripts": { diff --git a/tests.js b/tests.js index ba371e1..dacaaa6 100644 --- a/tests.js +++ b/tests.js @@ -50,4 +50,11 @@ securerandomstring({length: 256}, function(sr) { ); }); +securerandomstring({length: 256, urlsafe: true}, function(sr) { + test('generate a urlsafe random string 256 chars long', + sr.length, + 256 + ); +}); +