1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-02 10:20:05 +02:00

25 Commits

Author SHA1 Message Date
s2
388dd79a47 changelog 2024-09-26 13:37:30 +02:00
s2
bed8113750 1.1.4 2024-09-26 13:27:50 +02:00
s2
a23c85cabe update package.json 2024-09-26 13:22:31 +02:00
s2
af5b2eaced if the generated string is not long enough, retry 2024-09-26 13:18:22 +02:00
s2
51274ca17b add 10 chars test 2024-09-26 13:17:57 +02:00
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
s2
9fa7f94d57 update package version 2017-11-26 11:52:06 +01:00
s2
5ecefd381c 1.1.0 changelog 2017-11-26 11:51:57 +01:00
s2
50ae45ebd6 Merge branch 'master' of https://github.com/mrsangrin/secure-random-string into mrsangrin-master 2017-11-26 11:43:05 +01:00
Sandro Ariel Gómez Araya
1c0fec2544 Adding new contributor 2017-11-24 12:12:01 -03:00
Sandro Ariel Gómez Araya
6ef11ca1c8 Merge branch 'master' of github.com:mrsangrin/secure-random-string 2017-11-24 12:10:28 -03:00
Sandro Ariel Gómez Araya
e91153d961 Fix error in options name
* Change Readme.md fixing alphabetic by alphanumeric name of options
* Add test to new option alphanumeric always expect alphanumeric chars if 'alphanumeric' options is true
2017-11-24 12:07:54 -03:00
s2
275814d49a just nerdy whitespace stuff 2017-11-24 14:33:16 +01:00
Sandro Gomez
86caee2274 Merge branch 'master' into master 2017-11-24 10:25:02 -03:00
Sandro Ariel Gómez Araya
31839fea0b Improve Readme and code stylign
* Alphabetic options added into Readme.md
* Add empty space after 'if' in new option validation
2017-11-24 10:21:32 -03:00
s2
4689347480 fix source files to be compliant with .jscs rules 2017-11-24 09:48:03 +01:00
s2
a3cef28167 add jscsrc file so it's easier to contribute code 2017-11-24 09:47:48 +01:00
Sandro Ariel Gómez Araya
a1174300a8 Add alphanumeric option to generate token without non-alphanumeric chars 2017-11-13 11:51:42 -03:00
Mark Stosberg
00fa92269e Mention urlsafe removal int he Changelog. 2015-08-28 10:59:20 -04:00
6 changed files with 135 additions and 16 deletions

69
.jscsrc Normal file
View File

@@ -0,0 +1,69 @@
{
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true
},
"requireSpaceBetweenArguments": true,
"disallowMultipleVarDecl": true,
"requireVarDeclFirst": false,
"requireBlocksOnNewline": false,
"disallowEmptyBlocks": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"disallowDanglingUnderscores": false,
"requireCommaBeforeLineBreak": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": [
","
],
"requireSpacesInForStatement": true,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceAfterBinaryOperators": true,
"disallowKeywords": [
"with",
"continue"
],
"validateIndentation": "\t",
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowTrailingComma": true,
"disallowKeywordsOnNewLine": [
"else"
],
"requireLineFeedAtFileEnd": true,
"requireCapitalizedConstructors": true,
"requireDotNotation": false,
"disallowNewlineBeforeBlockStatements": true,
"disallowMultipleLineStrings": true,
"requireSpaceBeforeObjectValues": true
}

View File

@@ -4,6 +4,18 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
## [1.1.4] 2024-09-26
## Changes
- In previous versions, if there was not enough entropy on the system, `securerandomstring` would throw an exception. Now it will retry to generate a random string until it succeeds, or the call stack becomes full.
## [1.1.0] 2017-12-26
### New Features
- alphanumeric option to generate alphanumeric characters only.
## [1.0.0] 2015-08-27
### New Features
@@ -15,3 +27,5 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Async API now follows the Node.js convention of returning an error as the first argument
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)

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');
```
### 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
// Sync
@@ -34,7 +36,19 @@ srs({length: 256}, function(err, sr) {
console.log(sr);
});
```
### 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
@@ -47,9 +61,9 @@ the async API returns the error to the callback.
## Contributors
[Mark Stosberg](https://github.com/markstos)
[Mark Stosberg](https://github.com/markstos)
[Sandro Gomez](https://github.com/mrsangrin)
## License
[MIT](https://github.com/aheckmann/node-ses/blob/master/LICENSE)
[MIT](https://opensource.org/licenses/MIT)

View File

@@ -9,14 +9,14 @@ function srs(options, cb) {
}
var length = options['length'] || 32;
var alphanumeric = options['alphanumeric'] || false;
// async path
if (cb) {
crypto.randomBytes(length, function(err, buf) {
if (err) {
return cb(err);
}
return cb(null,_finish(buf));
return cb(null, _finish(buf));
});
}
// sync path
@@ -24,12 +24,18 @@ function srs(options, cb) {
return _finish(crypto.randomBytes(length));
}
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, '-');
}
if (string.length < length) {
return _finish(crypto.randomBytes(length));
}
return string.substr(0, length);
}
};

View File

@@ -1,6 +1,6 @@
{
"name": "secure-random-string",
"version": "1.0.0",
"version": "1.1.4",
"description": "Generates a secure random string with a given length",
"main": "lib/secure-random-string.js",
"scripts": {
@@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/S2-/securerandomstring.git"
"url": "https://github.com/5im-0n/securerandomstring.git"
},
"keywords": [
"crypto",
@@ -19,10 +19,12 @@
"token"
],
"author": "Simon Santoro",
"contributors": ["Mark Stosberg <mark@rideamigos.com>"],
"contributors": [
"Mark Stosberg <mark@rideamigos.com>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/S2-/securerandomstring/issues"
"url": "https://github.com/5im-0n/securerandomstring/issues"
},
"homepage": "https://github.com/S2-/securerandomstring"
"homepage": "https://github.com/5im-0n/securerandomstring"
}

View File

@@ -50,11 +50,25 @@ srs({length: 256}, function(err, sr) {
);
});
srs({alphanumeric: true}, function(err, sr) {;
test('Must contain alphanumeric only',
sr.match(/^[a-zA-Z0-9_]*$/g)[0] === sr,
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
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);
test('generate a random string 10 chars long (sync)', srs({length: 10}).length, 10);
//in 2000 chars there should be at least one substitution