secure-random-string
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -7,2 +7,8 @@ | ||
## [1.1.0] 2017-12-26 | ||
### New Features | ||
- alphanumeric option to generate alphanumeric characters only. | ||
## [1.0.0] 2015-08-27 | ||
@@ -19,1 +25,3 @@ | ||
to the callback. The error might be populated if the system runs out of entropy. (#2, @markstos) | ||
- `urlsafe` option was removed. All strings are URL-safe now. (#4, @S2-, @markstos) |
@@ -12,3 +12,3 @@ var crypto = require('crypto'); | ||
var length = options['length'] || 32; | ||
var alphanumeric = options['alphanumeric'] || false; | ||
// async path | ||
@@ -20,3 +20,3 @@ if (cb) { | ||
} | ||
return cb(null,_finish(buf)); | ||
return cb(null, _finish(buf)); | ||
}); | ||
@@ -29,8 +29,11 @@ } | ||
function _finish (buf) { | ||
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); | ||
} | ||
}; | ||
@@ -37,0 +40,0 @@ |
{ | ||
"name": "secure-random-string", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Generates a secure random string with a given length", | ||
@@ -5,0 +5,0 @@ "main": "lib/secure-random-string.js", |
@@ -37,3 +37,15 @@ # secure-random-string | ||
``` | ||
### Options: alphanumeric | ||
Optionally, you can specify a 'alphanumeric' option to get a alphanumerical chars only. | ||
```javascript | ||
// sync | ||
var result = srs({alphanumeric: true}); | ||
// async | ||
srs({alphanumeric: true}, function(err, sr) { | ||
console.log(sr); | ||
}); | ||
``` | ||
## Error handling | ||
@@ -52,2 +64,3 @@ | ||
[Mark Stosberg](https://github.com/markstos) | ||
[Sandro Gomez](https://github.com/mrsangrin) | ||
@@ -57,2 +70,1 @@ ## License | ||
[MIT](https://github.com/aheckmann/node-ses/blob/master/LICENSE) | ||
11
tests.js
@@ -53,7 +53,12 @@ var srs = require('./lib/secure-random-string.js'); | ||
srs({alphanumeric: true}, function(err, sr) {; | ||
test('Must contain alphanumeric only', | ||
sr.match(/^[a-zA-Z0-9_]*$/g)[0] === sr, | ||
true | ||
); | ||
}); | ||
// sync tests | ||
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 256 chars long (sync)', srs({length:256}).length, 256); | ||
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); | ||
@@ -60,0 +65,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6912
90
68