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

7 Commits

Author SHA1 Message Date
s2
f7e565bcc7 1.1.3 2020-04-08 18:20:02 +02:00
s2
f82de05f4b throw exception if string is not selected length 2020-04-08 18:19:54 +02:00
s2
94f2eadf63 1.1.2 2019-07-13 15:44:08 +02:00
s2
394d13d5a7 1.1.1 2019-07-13 15:43:41 +02:00
Mark Stosberg
a6dc243138 Clarify that the default char set returned is base64 encoded
Also, link to a neutral URL for the MIT license instead of the node-ses repo.
2019-07-12 09:20:40 -04:00
s2
cb6bbee3b1 add one more test 2017-11-26 22:04:14 +01:00
s2
0259480474 just a newline 2017-11-26 21:57:41 +01:00
4 changed files with 20 additions and 5 deletions

View File

@@ -8,7 +8,9 @@ Node.js module that generates a cryptographically secure random string with a gi
var srs = require('secure-random-string'); var srs = require('secure-random-string');
``` ```
### Default behavior: Generate a random string 32 characters long. ### Default behavior: Generate a random Base64 encoded string 32 characters long.
This may include alphanumeric characters as well as the following characters: +, /, =.
```javascript ```javascript
// Sync // Sync
@@ -59,9 +61,9 @@ the async API returns the error to the callback.
## Contributors ## Contributors
[Mark Stosberg](https://github.com/markstos) [Mark Stosberg](https://github.com/markstos)
[Sandro Gomez](https://github.com/mrsangrin) [Sandro Gomez](https://github.com/mrsangrin)
## License ## License
[MIT](https://github.com/aheckmann/node-ses/blob/master/LICENSE) [MIT](https://opensource.org/licenses/MIT)

View File

@@ -31,6 +31,9 @@ function srs(options, cb) {
} else { } else {
string = string.replace(/\//g, '_').replace(/\+/g, '-'); string = string.replace(/\//g, '_').replace(/\+/g, '-');
} }
if (string.length < length) {
throw new Error(`Generated string is too short. Please catch this Error and try again.`);
}
return string.substr(0, length); return string.substr(0, length);
} }
}; };

View File

@@ -1,6 +1,6 @@
{ {
"name": "secure-random-string", "name": "secure-random-string",
"version": "1.1.0", "version": "1.1.3",
"description": "Generates a secure random string with a given length", "description": "Generates a secure random string with a given length",
"main": "lib/secure-random-string.js", "main": "lib/secure-random-string.js",
"scripts": { "scripts": {
@@ -19,7 +19,9 @@
"token" "token"
], ],
"author": "Simon Santoro", "author": "Simon Santoro",
"contributors": ["Mark Stosberg <mark@rideamigos.com>"], "contributors": [
"Mark Stosberg <mark@rideamigos.com>"
],
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/S2-/securerandomstring/issues" "url": "https://github.com/S2-/securerandomstring/issues"

View File

@@ -56,6 +56,14 @@ srs({alphanumeric: true}, function(err, sr) {;
true true
); );
}); });
srs({alphanumeric: true, length: 40}, function(err, sr) {;
test('Must contain alphanumeric only and be 40 chars long',
sr.match(/^[a-zA-Z0-9_]*$/g)[0] === sr && sr.length === 40,
true
);
});
// sync tests // sync tests
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);