mirror of
https://github.com/S2-/securerandomstring.git
synced 2025-08-02 18:30:04 +02:00
Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
bed8113750 | |||
a23c85cabe | |||
af5b2eaced | |||
51274ca17b | |||
f7e565bcc7 | |||
f82de05f4b | |||
94f2eadf63 | |||
394d13d5a7 | |||
![]() |
a6dc243138 | ||
cb6bbee3b1 | |||
0259480474 | |||
9fa7f94d57 | |||
5ecefd381c | |||
50ae45ebd6 | |||
![]() |
1c0fec2544 | ||
![]() |
6ef11ca1c8 | ||
![]() |
e91153d961 | ||
275814d49a | |||
![]() |
86caee2274 | ||
![]() |
31839fea0b | ||
4689347480 | |||
a3cef28167 | |||
![]() |
a1174300a8 | ||
![]() |
00fa92269e |
69
.jscsrc
Normal file
69
.jscsrc
Normal 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
|
||||||
|
}
|
@@ -4,6 +4,12 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
This project adheres to [Semantic Versioning](http://semver.org/).
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
## [1.1.0] 2017-12-26
|
||||||
|
|
||||||
|
### New Features
|
||||||
|
|
||||||
|
- alphanumeric option to generate alphanumeric characters only.
|
||||||
|
|
||||||
## [1.0.0] 2015-08-27
|
## [1.0.0] 2015-08-27
|
||||||
|
|
||||||
### New Features
|
### New Features
|
||||||
@@ -15,3 +21,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
|
- 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)
|
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)
|
||||||
|
20
README.md
20
README.md
@@ -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
|
||||||
@@ -34,7 +36,19 @@ srs({length: 256}, function(err, sr) {
|
|||||||
console.log(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
|
## Error handling
|
||||||
|
|
||||||
|
|
||||||
@@ -48,8 +62,8 @@ 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)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[MIT](https://github.com/aheckmann/node-ses/blob/master/LICENSE)
|
[MIT](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
@@ -9,7 +9,7 @@ function srs(options, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var length = options['length'] || 32;
|
var length = options['length'] || 32;
|
||||||
|
var alphanumeric = options['alphanumeric'] || false;
|
||||||
// async path
|
// async path
|
||||||
if (cb) {
|
if (cb) {
|
||||||
crypto.randomBytes(length, function(err, buf) {
|
crypto.randomBytes(length, function(err, buf) {
|
||||||
@@ -26,10 +26,16 @@ function srs(options, cb) {
|
|||||||
|
|
||||||
function _finish(buf) {
|
function _finish(buf) {
|
||||||
var string = buf.toString('base64');
|
var string = buf.toString('base64');
|
||||||
|
if (alphanumeric === true) {
|
||||||
|
string = string.replace(/[\W_]+/g, '');
|
||||||
|
} else {
|
||||||
string = string.replace(/\//g, '_').replace(/\+/g, '-');
|
string = string.replace(/\//g, '_').replace(/\+/g, '-');
|
||||||
|
}
|
||||||
|
if (string.length < length) {
|
||||||
|
return _finish(crypto.randomBytes(length));
|
||||||
|
}
|
||||||
return string.substr(0, length);
|
return string.substr(0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
12
package.json
12
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "secure-random-string",
|
"name": "secure-random-string",
|
||||||
"version": "1.0.0",
|
"version": "1.1.4",
|
||||||
"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": {
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/S2-/securerandomstring.git"
|
"url": "https://github.com/5im-0n/securerandomstring.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"crypto",
|
"crypto",
|
||||||
@@ -19,10 +19,12 @@
|
|||||||
"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/5im-0n/securerandomstring/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/S2-/securerandomstring"
|
"homepage": "https://github.com/5im-0n/securerandomstring"
|
||||||
}
|
}
|
||||||
|
14
tests.js
14
tests.js
@@ -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
|
// 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);
|
||||||
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 random string 10 chars long (sync)', srs({length: 10}).length, 10);
|
||||||
|
|
||||||
|
|
||||||
//in 2000 chars there should be at least one substitution
|
//in 2000 chars there should be at least one substitution
|
||||||
|
Reference in New Issue
Block a user