Skip to content

Commit 4a52695

Browse files
committed
use cryptographically secure random function
1 parent 833f31a commit 4a52695

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

.verb.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ var randomize = require('{%= name %}');
88

99
```js
1010
randomize(pattern, length, options);
11+
randomize.isCrypto;
1112
```
1213

1314
- `pattern` **{String}**: (required) The pattern to use for randomizing
1415
- `length` **{Number}**: (optional) The length of the string to generate
1516
- `options` **{Object}**: (optional) See available [options](#options)
1617

18+
- `randomize.isCrypto` will be `true` when a cryptographically secure function is being used to generate random numbers. The value will be `false` when the function in use is `Math.random`.
1719

1820
### pattern
1921

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
var isNumber = require('is-number');
1111
var typeOf = require('kind-of');
12+
var mathRandom = require('math-random');
1213

1314
/**
1415
* Expose `randomatic`
1516
*/
1617

1718
module.exports = randomatic;
19+
module.exports.isCrypto = !!mathRandom.cryptographic;
1820

1921
/**
2022
* Available mask characters
@@ -78,7 +80,7 @@ function randomatic(pattern, length, options) {
7880
if (custom) mask += pattern;
7981

8082
while (length--) {
81-
res += mask.charAt(parseInt(Math.random() * mask.length, 10));
83+
res += mask.charAt(parseInt(mathRandom() * mask.length, 10));
8284
}
8385
return res;
8486
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
},
3131
"dependencies": {
3232
"is-number": "^4.0.0",
33-
"kind-of": "^6.0.0"
33+
"kind-of": "^6.0.0",
34+
"math-random": "^1.0.1"
3435
},
3536
"devDependencies": {
3637
"ansi-bold": "^0.1.1",

test.js

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function test(re, str) {
1616
}
1717

1818
describe('randomatic', function() {
19+
it('should export an isCrypto boolean property', function() {
20+
assert.equal(typeof randomize.isCrypto, 'boolean');
21+
});
22+
1923
it('should throw an error when no arguments are passed:', function() {
2024
assert.throws(function() {
2125
randomize();

0 commit comments

Comments
 (0)