1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-02 18:30:04 +02:00

make urlsafe the default

This commit is contained in:
s2
2015-08-27 23:31:32 +02:00
parent f4c0783cfd
commit 9a72a2cfe2
3 changed files with 18 additions and 4 deletions

View File

@@ -25,8 +25,8 @@ srs(function(err, sr) {
Optionally, you can specify a 'length' option to specify a length. Optionally, you can specify a 'length' option to specify a length.
The 'urlsafe' option replaces a potential `+` character with `-` and the `/` character When 'urlsafe' is true (the default), potential `+` and `/` characters are replaced respectively with `-` and `_`.
with `_`, created a valid [base64url](https://en.wikipedia.org/wiki/Base64) format string. This is a valid [base64url](https://en.wikipedia.org/wiki/Base64) string.
```javascript ```javascript
// sync // sync

View File

@@ -7,6 +7,8 @@ function srs(options, cb) {
} else { } else {
options = options || {}; options = options || {};
} }
options.urlsafe = typeof(options.urlsafe) === 'boolean' ? options.urlsafe : true;
var length = options['length'] || 32; var length = options['length'] || 32;
// async path // async path

View File

@@ -50,7 +50,7 @@ srs({length: 256}, function(err, sr) {
); );
}); });
srs({length: 256, urlsafe: true}, function(err, sr) { srs({length: 256}, function(err, sr) {
test('generate a urlsafe random string 256 chars long', test('generate a urlsafe random string 256 chars long',
sr.length, sr.length,
256 256
@@ -62,4 +62,16 @@ srs({length: 256, urlsafe: true}, function(err, sr) {
test('generate a random string 32 chars long (sync)', srs().length, 32); test('generate a random string 32 chars long (sync)', srs().length, 32);
test('generate a random string 1 chars long (sync)', srs({length:1}).length, 1); test('generate a random string 1 chars long (sync)', srs({length:1}).length, 1);
test('generate a random string 256 chars long (sync)', srs({length:256}).length, 256); test('generate a random string 256 chars long (sync)', srs({length:256}).length, 256);
test('generate a urlsafe random string 256 chars long (sync)', srs({length:256, urlsafe:true}).length, 256); test('generate a urlsafe random string 256 chars long (sync)', srs({length:256}).length, 256);
//when generating, the default should be urlsafe
//in 2000 chars there should be at least one substitution
test('check that the random string is urlsafe by default', (function() {
var s = srs({length: 2000});
return s.indexOf('+') + s.indexOf('/') === -2;
})(), true);
test('check that the useless urlsafe: false option works', (function() {
var s = srs({length: 2000, urlsafe: false});
return s.indexOf('_') + s.indexOf('-') === -2;
})(), true);