You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// `crypto.getRandomValues` throws an error if too much entropy is requested at once. (https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#exceptions)
// Generating entropy is faster than complex math operations, so we use the simplest way
9
11
constcharacterCount=characters.length;
10
-
constmaxValidSelector=(Math.floor(0x10000/characterCount)*characterCount)-1;// Using values above this will ruin distribution when using modular division
12
+
constmaxValidSelector=(Math.floor(0x1_00_00/characterCount)*characterCount)-1;// Using values above this will ruin distribution when using modular division
11
13
constentropyLength=2*Math.ceil(1.1*length);// Generating a bit more than required so chances we need more than one pass will be really low
// Generating entropy is faster than complex math operations, so we use the simplest way
36
38
constcharacterCount=characters.length;
37
-
constmaxValidSelector=(Math.floor(0x10000/characterCount)*characterCount)-1;// Using values above this will ruin distribution when using modular division
39
+
constmaxValidSelector=(Math.floor(0x1_00_00/characterCount)*characterCount)-1;// Using values above this will ruin distribution when using modular division
38
40
constentropyLength=2*Math.ceil(1.1*length);// Generating a bit more than required so chances we need more than one pass will be really low
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
86
+
/**
87
+
Asynchronously generate a [cryptographically strong](https://en.wikipedia.org/wiki/Strong_cryptography) random string.
91
88
92
-
> The `crypto.randomBytes()` method will not complete until there is sufficient entropy available. This should normally never take longer than a few milliseconds. The only time when generating the random bytes may conceivably block for a longer period of time is right after boot, when the whole system is still low on entropy.
89
+
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
93
90
94
-
In general, anything async comes with some overhead on it's own.
91
+
> The `crypto.randomBytes()` method will not complete until there is sufficient entropy available. This should normally never take longer than a few milliseconds. The only time when generating the random bytes may conceivably block for a longer period of time is right after boot, when the whole system is still low on entropy.
95
92
96
-
@returns A promise which resolves to a randomized string.
93
+
In general, anything async comes with some overhead on it's own.
97
94
98
-
@example
99
-
```
100
-
import cryptoRandomString from 'crypto-random-string';
95
+
@returns A promise which resolves to a randomized string.
101
96
102
-
await cryptoRandomString.async({length: 10});
103
-
//=> '2cf05d94db'
104
-
```
105
-
*/
106
-
async(options: Options): Promise<string>;
107
-
};
97
+
@example
98
+
```
99
+
import {cryptoRandomStringAsync} from 'crypto-random-string';
0 commit comments