@depup/crypto-random-string 5.0.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/browser.js +31 -0
- package/changes.json +10 -0
- package/core.js +140 -0
- package/index.d.ts +105 -0
- package/index.js +13 -0
- package/license +9 -0
- package/package.json +75 -0
- package/readme.md +125 -0
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @depup/crypto-random-string
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [crypto-random-string](https://www.npmjs.com/package/crypto-random-string)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/crypto-random-string
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [crypto-random-string](https://www.npmjs.com/package/crypto-random-string) @ 5.0.0 |
|
|
17
|
+
| Processed | 2026-03-17 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 1 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| type-fest | ^2.12.2 | ^5.4.4 |
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/crypto-random-string
|
|
30
|
+
|
|
31
|
+
License inherited from the original package.
|
package/browser.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
import {createStringGenerator, createAsyncStringGenerator} from './core.js';
|
|
3
|
+
|
|
4
|
+
const toHex = uInt8Array => [...uInt8Array].map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
5
|
+
const toBase64 = uInt8Array => btoa(String.fromCodePoint(...uInt8Array));
|
|
6
|
+
|
|
7
|
+
// `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)
|
|
8
|
+
const maxEntropy = 65_536;
|
|
9
|
+
|
|
10
|
+
function getRandomValues(byteLength) {
|
|
11
|
+
const generatedBytes = new Uint8Array(byteLength);
|
|
12
|
+
|
|
13
|
+
for (let totalGeneratedBytes = 0; totalGeneratedBytes < byteLength; totalGeneratedBytes += maxEntropy) {
|
|
14
|
+
generatedBytes.set(
|
|
15
|
+
crypto.getRandomValues(new Uint8Array(Math.min(maxEntropy, byteLength - totalGeneratedBytes))),
|
|
16
|
+
totalGeneratedBytes,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return generatedBytes;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function specialRandomBytes(byteLength, type, length) {
|
|
24
|
+
const generatedBytes = getRandomValues(byteLength);
|
|
25
|
+
const convert = type === 'hex' ? toHex : toBase64;
|
|
26
|
+
|
|
27
|
+
return convert(generatedBytes).slice(0, length);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default createStringGenerator(specialRandomBytes, getRandomValues);
|
|
31
|
+
export const cryptoRandomStringAsync = createAsyncStringGenerator(specialRandomBytes, getRandomValues);
|
package/changes.json
ADDED
package/core.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const urlSafeCharacters = [...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'];
|
|
2
|
+
const numericCharacters = [...'0123456789'];
|
|
3
|
+
const distinguishableCharacters = [...'CDEHKMPRTUWXY012458'];
|
|
4
|
+
const asciiPrintableCharacters = [...'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'];
|
|
5
|
+
const alphanumericCharacters = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'];
|
|
6
|
+
|
|
7
|
+
const readUInt16LE = (uInt8Array, offset) => uInt8Array[offset] + (uInt8Array[offset + 1] << 8); // eslint-disable-line no-bitwise
|
|
8
|
+
|
|
9
|
+
const generateForCustomCharacters = (length, characters, randomBytes) => {
|
|
10
|
+
// Generating entropy is faster than complex math operations, so we use the simplest way
|
|
11
|
+
const characterCount = characters.length;
|
|
12
|
+
const maxValidSelector = (Math.floor(0x1_00_00 / characterCount) * characterCount) - 1; // Using values above this will ruin distribution when using modular division
|
|
13
|
+
const entropyLength = 2 * Math.ceil(1.1 * length); // Generating a bit more than required so chances we need more than one pass will be really low
|
|
14
|
+
let string = '';
|
|
15
|
+
let stringLength = 0;
|
|
16
|
+
|
|
17
|
+
while (stringLength < length) { // In case we had many bad values, which may happen for character sets of size above 0x8000 but close to it
|
|
18
|
+
const entropy = randomBytes(entropyLength);
|
|
19
|
+
let entropyPosition = 0;
|
|
20
|
+
|
|
21
|
+
while (entropyPosition < entropyLength && stringLength < length) {
|
|
22
|
+
const entropyValue = readUInt16LE(entropy, entropyPosition);
|
|
23
|
+
entropyPosition += 2;
|
|
24
|
+
if (entropyValue > maxValidSelector) { // Skip values which will ruin distribution when using modular division
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
string += characters[entropyValue % characterCount];
|
|
29
|
+
stringLength++;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const generateForCustomCharactersAsync = async (length, characters, randomBytesAsync) => {
|
|
37
|
+
// Generating entropy is faster than complex math operations, so we use the simplest way
|
|
38
|
+
const characterCount = characters.length;
|
|
39
|
+
const maxValidSelector = (Math.floor(0x1_00_00 / characterCount) * characterCount) - 1; // Using values above this will ruin distribution when using modular division
|
|
40
|
+
const entropyLength = 2 * Math.ceil(1.1 * length); // Generating a bit more than required so chances we need more than one pass will be really low
|
|
41
|
+
let string = '';
|
|
42
|
+
let stringLength = 0;
|
|
43
|
+
|
|
44
|
+
while (stringLength < length) { // In case we had many bad values, which may happen for character sets of size above 0x8000 but close to it
|
|
45
|
+
const entropy = await randomBytesAsync(entropyLength); // eslint-disable-line no-await-in-loop
|
|
46
|
+
let entropyPosition = 0;
|
|
47
|
+
|
|
48
|
+
while (entropyPosition < entropyLength && stringLength < length) {
|
|
49
|
+
const entropyValue = readUInt16LE(entropy, entropyPosition);
|
|
50
|
+
entropyPosition += 2;
|
|
51
|
+
if (entropyValue > maxValidSelector) { // Skip values which will ruin distribution when using modular division
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
string += characters[entropyValue % characterCount];
|
|
56
|
+
stringLength++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const allowedTypes = new Set([
|
|
64
|
+
undefined,
|
|
65
|
+
'hex',
|
|
66
|
+
'base64',
|
|
67
|
+
'url-safe',
|
|
68
|
+
'numeric',
|
|
69
|
+
'distinguishable',
|
|
70
|
+
'ascii-printable',
|
|
71
|
+
'alphanumeric',
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const createGenerator = (generateForCustomCharacters, specialRandomBytes, randomBytes) => ({length, type, characters}) => {
|
|
75
|
+
if (!(length >= 0 && Number.isFinite(length))) {
|
|
76
|
+
throw new TypeError('Expected a `length` to be a non-negative finite number');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (type !== undefined && characters !== undefined) {
|
|
80
|
+
throw new TypeError('Expected either `type` or `characters`');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (characters !== undefined && typeof characters !== 'string') {
|
|
84
|
+
throw new TypeError('Expected `characters` to be string');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!allowedTypes.has(type)) {
|
|
88
|
+
throw new TypeError(`Unknown type: ${type}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (type === undefined && characters === undefined) {
|
|
92
|
+
type = 'hex';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (type === 'hex' || (type === undefined && characters === undefined)) {
|
|
96
|
+
return specialRandomBytes(Math.ceil(length * 0.5), 'hex', length); // Needs 0.5 bytes of entropy per character
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (type === 'base64') {
|
|
100
|
+
return specialRandomBytes(Math.ceil(length * 0.75), 'base64', length); // Needs 0.75 bytes of entropy per character
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (type === 'url-safe') {
|
|
104
|
+
return generateForCustomCharacters(length, urlSafeCharacters, randomBytes);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (type === 'numeric') {
|
|
108
|
+
return generateForCustomCharacters(length, numericCharacters, randomBytes);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (type === 'distinguishable') {
|
|
112
|
+
return generateForCustomCharacters(length, distinguishableCharacters, randomBytes);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (type === 'ascii-printable') {
|
|
116
|
+
return generateForCustomCharacters(length, asciiPrintableCharacters, randomBytes);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (type === 'alphanumeric') {
|
|
120
|
+
return generateForCustomCharacters(length, alphanumericCharacters, randomBytes);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (characters.length === 0) {
|
|
124
|
+
throw new TypeError('Expected `characters` string length to be greater than or equal to 1');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (characters.length > 0x1_00_00) {
|
|
128
|
+
throw new TypeError('Expected `characters` string length to be less or equal to 65536');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return generateForCustomCharacters(length, characters, randomBytes);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export function createStringGenerator(specialRandomBytes, randomBytes) {
|
|
135
|
+
return createGenerator(generateForCustomCharacters, specialRandomBytes, randomBytes);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function createAsyncStringGenerator(specialRandomBytesAsync, randomBytesAsync) {
|
|
139
|
+
return createGenerator(generateForCustomCharactersAsync, specialRandomBytesAsync, randomBytesAsync);
|
|
140
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {MergeExclusive} from 'type-fest';
|
|
2
|
+
|
|
3
|
+
interface BaseOptions {
|
|
4
|
+
/**
|
|
5
|
+
Length of the returned string.
|
|
6
|
+
*/
|
|
7
|
+
length: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface TypeOption {
|
|
11
|
+
/**
|
|
12
|
+
Use only characters from a predefined set of allowed characters.
|
|
13
|
+
|
|
14
|
+
Cannot be set at the same time as the `characters` option.
|
|
15
|
+
|
|
16
|
+
@default 'hex'
|
|
17
|
+
|
|
18
|
+
The `distinguishable` set contains only uppercase characters that are not easily confused: `CDEHKMPRTUWXY012458`. It can be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors. For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.
|
|
19
|
+
|
|
20
|
+
The `ascii-printable` set contains all [printable ASCII characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): ``!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~`` Useful for generating passwords where all possible ASCII characters should be used.
|
|
21
|
+
|
|
22
|
+
The `alphanumeric` set contains uppercase letters, lowercase letters, and digits: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`. Useful for generating [nonce](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/nonce) values.
|
|
23
|
+
|
|
24
|
+
@example
|
|
25
|
+
```
|
|
26
|
+
cryptoRandomString({length: 10});
|
|
27
|
+
//=> '87fc70e2b9'
|
|
28
|
+
|
|
29
|
+
cryptoRandomString({length: 10, type: 'base64'});
|
|
30
|
+
//=> 'mhsX7xmIv/'
|
|
31
|
+
|
|
32
|
+
cryptoRandomString({length: 10, type: 'url-safe'});
|
|
33
|
+
//=> 'VEjfNW3Yej'
|
|
34
|
+
|
|
35
|
+
cryptoRandomString({length: 10, type: 'numeric'});
|
|
36
|
+
//=> '8314659141'
|
|
37
|
+
|
|
38
|
+
cryptoRandomString({length: 6, type: 'distinguishable'});
|
|
39
|
+
//=> 'CDEHKM'
|
|
40
|
+
|
|
41
|
+
cryptoRandomString({length: 10, type: 'ascii-printable'});
|
|
42
|
+
//=> '`#Rt8$IK>B'
|
|
43
|
+
|
|
44
|
+
cryptoRandomString({length: 10, type: 'alphanumeric'});
|
|
45
|
+
//=> 'DMuKL8YtE7'
|
|
46
|
+
```
|
|
47
|
+
*/
|
|
48
|
+
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface CharactersOption {
|
|
52
|
+
/**
|
|
53
|
+
Use only characters from a custom set of allowed characters.
|
|
54
|
+
|
|
55
|
+
Cannot be set at the same time as the `type` option.
|
|
56
|
+
|
|
57
|
+
Minimum length: `1`
|
|
58
|
+
Maximum length: `65536`
|
|
59
|
+
|
|
60
|
+
@example
|
|
61
|
+
```
|
|
62
|
+
cryptoRandomString({length: 10, characters: '0123456789'});
|
|
63
|
+
//=> '8796225811'
|
|
64
|
+
```
|
|
65
|
+
*/
|
|
66
|
+
characters?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type Options = BaseOptions & MergeExclusive<TypeOption, CharactersOption>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
Generate a [cryptographically strong](https://en.wikipedia.org/wiki/Strong_cryptography) random string.
|
|
73
|
+
|
|
74
|
+
@returns A randomized string.
|
|
75
|
+
|
|
76
|
+
@example
|
|
77
|
+
```
|
|
78
|
+
import cryptoRandomString from 'crypto-random-string';
|
|
79
|
+
|
|
80
|
+
cryptoRandomString({length: 10});
|
|
81
|
+
//=> '2cf05d94db'
|
|
82
|
+
```
|
|
83
|
+
*/
|
|
84
|
+
export default function cryptoRandomString(options: Options): string;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
Asynchronously generate a [cryptographically strong](https://en.wikipedia.org/wiki/Strong_cryptography) random string.
|
|
88
|
+
|
|
89
|
+
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
|
|
90
|
+
|
|
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.
|
|
92
|
+
|
|
93
|
+
In general, anything async comes with some overhead on it's own.
|
|
94
|
+
|
|
95
|
+
@returns A promise which resolves to a randomized string.
|
|
96
|
+
|
|
97
|
+
@example
|
|
98
|
+
```
|
|
99
|
+
import {cryptoRandomStringAsync} from 'crypto-random-string';
|
|
100
|
+
|
|
101
|
+
await cryptoRandomStringAsync({length: 10});
|
|
102
|
+
//=> '2cf05d94db'
|
|
103
|
+
```
|
|
104
|
+
*/
|
|
105
|
+
export function cryptoRandomStringAsync(options: Options): Promise<string>;
|
package/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// TODO: When targeting Node.js 16, remove `cryptoRandomStringAsync` and use `crypto.webcrypto.getRandomValues` to interop with the browser code.
|
|
2
|
+
// TODO: Later, when targeting Node.js 18, only use the browser code
|
|
3
|
+
import {promisify} from 'node:util';
|
|
4
|
+
import {randomBytes} from 'node:crypto';
|
|
5
|
+
import {createStringGenerator, createAsyncStringGenerator} from './core.js';
|
|
6
|
+
|
|
7
|
+
const randomBytesAsync = promisify(randomBytes);
|
|
8
|
+
|
|
9
|
+
export default createStringGenerator((byteLength, type, length) => randomBytes(byteLength).toString(type).slice(0, length), size => new Uint8Array(randomBytes(size)));
|
|
10
|
+
export const cryptoRandomStringAsync = createAsyncStringGenerator(async (byteLength, type, length) => {
|
|
11
|
+
const buffer = await randomBytesAsync(byteLength);
|
|
12
|
+
return buffer.toString(type).slice(0, length);
|
|
13
|
+
}, async size => new Uint8Array(await randomBytesAsync(size)));
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@depup/crypto-random-string",
|
|
3
|
+
"version": "5.0.0-depup.0",
|
|
4
|
+
"description": "[DepUp] Generate a cryptographically strong random string",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "sindresorhus/crypto-random-string",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Sindre Sorhus",
|
|
10
|
+
"email": "sindresorhus@gmail.com",
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"node": "./index.js",
|
|
17
|
+
"browser": "./browser.js"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=14.16"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "xo && ava && tsd"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.js",
|
|
27
|
+
"browser.js",
|
|
28
|
+
"core.js",
|
|
29
|
+
"index.d.ts",
|
|
30
|
+
"changes.json",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"keywords": [
|
|
34
|
+
"depup",
|
|
35
|
+
"dependency-bumped",
|
|
36
|
+
"updated-deps",
|
|
37
|
+
"crypto-random-string",
|
|
38
|
+
"random",
|
|
39
|
+
"string",
|
|
40
|
+
"text",
|
|
41
|
+
"id",
|
|
42
|
+
"identifier",
|
|
43
|
+
"slug",
|
|
44
|
+
"salt",
|
|
45
|
+
"pin",
|
|
46
|
+
"crypto",
|
|
47
|
+
"strong",
|
|
48
|
+
"secure",
|
|
49
|
+
"hex",
|
|
50
|
+
"secret",
|
|
51
|
+
"protect"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"type-fest": "^5.4.4"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"ava": "^4.2.0",
|
|
58
|
+
"dot-prop": "^7.2.0",
|
|
59
|
+
"tsd": "^0.20.0",
|
|
60
|
+
"xo": "^0.48.0"
|
|
61
|
+
},
|
|
62
|
+
"depup": {
|
|
63
|
+
"changes": {
|
|
64
|
+
"type-fest": {
|
|
65
|
+
"from": "^2.12.2",
|
|
66
|
+
"to": "^5.4.4"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"depsUpdated": 1,
|
|
70
|
+
"originalPackage": "crypto-random-string",
|
|
71
|
+
"originalVersion": "5.0.0",
|
|
72
|
+
"processedAt": "2026-03-17T16:35:11.633Z",
|
|
73
|
+
"smokeTest": "passed"
|
|
74
|
+
}
|
|
75
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# crypto-random-string
|
|
2
|
+
|
|
3
|
+
> Generate a [cryptographically strong](https://en.wikipedia.org/wiki/Strong_cryptography) random string
|
|
4
|
+
|
|
5
|
+
Can be useful for creating an identifier, slug, salt, PIN code, fixture, etc.
|
|
6
|
+
|
|
7
|
+
Works in Node.js and browsers.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install crypto-random-string
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import cryptoRandomString from 'crypto-random-string';
|
|
19
|
+
|
|
20
|
+
cryptoRandomString({length: 10});
|
|
21
|
+
//=> '2cf05d94db'
|
|
22
|
+
|
|
23
|
+
cryptoRandomString({length: 10, type: 'base64'});
|
|
24
|
+
//=> 'YMiMbaQl6I'
|
|
25
|
+
|
|
26
|
+
cryptoRandomString({length: 10, type: 'url-safe'});
|
|
27
|
+
//=> 'YN-tqc8pOw'
|
|
28
|
+
|
|
29
|
+
cryptoRandomString({length: 10, type: 'numeric'});
|
|
30
|
+
//=> '8314659141'
|
|
31
|
+
|
|
32
|
+
cryptoRandomString({length: 6, type: 'distinguishable'});
|
|
33
|
+
//=> 'CDEHKM'
|
|
34
|
+
|
|
35
|
+
cryptoRandomString({length: 10, type: 'ascii-printable'});
|
|
36
|
+
//=> '`#Rt8$IK>B'
|
|
37
|
+
|
|
38
|
+
cryptoRandomString({length: 10, type: 'alphanumeric'});
|
|
39
|
+
//=> 'DMuKL8YtE7'
|
|
40
|
+
|
|
41
|
+
cryptoRandomString({length: 10, characters: 'abc'});
|
|
42
|
+
//=> 'abaaccabac'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### cryptoRandomString(options)
|
|
48
|
+
|
|
49
|
+
Returns a randomized string. [Hex](https://en.wikipedia.org/wiki/Hexadecimal) by default.
|
|
50
|
+
|
|
51
|
+
### cryptoRandomStringAsync(options)
|
|
52
|
+
|
|
53
|
+
Returns a promise which resolves to a randomized string. [Hex](https://en.wikipedia.org/wiki/Hexadecimal) by default.
|
|
54
|
+
|
|
55
|
+
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
|
|
56
|
+
|
|
57
|
+
> 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.
|
|
58
|
+
|
|
59
|
+
In general, anything async comes with some overhead on it's own.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import {cryptoRandomStringAsync} from 'crypto-random-string';
|
|
63
|
+
|
|
64
|
+
await cryptoRandomStringAsync({length: 10});
|
|
65
|
+
//=> '2cf05d94db'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### options
|
|
69
|
+
|
|
70
|
+
Type: `object`
|
|
71
|
+
|
|
72
|
+
##### length
|
|
73
|
+
|
|
74
|
+
*Required*\
|
|
75
|
+
Type: `number`
|
|
76
|
+
|
|
77
|
+
Length of the returned string.
|
|
78
|
+
|
|
79
|
+
##### type
|
|
80
|
+
|
|
81
|
+
Type: `string`\
|
|
82
|
+
Default: `'hex'`\
|
|
83
|
+
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric'`
|
|
84
|
+
|
|
85
|
+
Use only characters from a predefined set of allowed characters.
|
|
86
|
+
|
|
87
|
+
Cannot be set at the same time as the `characters` option.
|
|
88
|
+
|
|
89
|
+
The `distinguishable` set contains only uppercase characters that are not easily confused: `CDEHKMPRTUWXY012458`. It can be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors. For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.
|
|
90
|
+
|
|
91
|
+
The `ascii-printable` set contains all [printable ASCII characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): ``!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~`` Useful for generating passwords where all possible ASCII characters should be used.
|
|
92
|
+
|
|
93
|
+
The `alphanumeric` set contains uppercase letters, lowercase letters, and digits: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`. Useful for generating [nonce](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/nonce) values.
|
|
94
|
+
|
|
95
|
+
##### characters
|
|
96
|
+
|
|
97
|
+
Type: `string`\
|
|
98
|
+
Minimum length: `1`\
|
|
99
|
+
Maximum length: `65536`
|
|
100
|
+
|
|
101
|
+
Use only characters from a custom set of allowed characters.
|
|
102
|
+
|
|
103
|
+
Cannot be set at the same time as the `type` option.
|
|
104
|
+
|
|
105
|
+
## Related
|
|
106
|
+
|
|
107
|
+
- [random-int](https://github.com/sindresorhus/random-int) - Generate a random integer
|
|
108
|
+
- [random-float](https://github.com/sindresorhus/random-float) - Generate a random float
|
|
109
|
+
- [random-item](https://github.com/sindresorhus/random-item) - Get a random item from an array
|
|
110
|
+
- [random-boolean](https://github.com/arthurvr/random-boolean) - Get a random boolean
|
|
111
|
+
- [random-obj-key](https://github.com/sindresorhus/random-obj-key) - Get a random key from an object
|
|
112
|
+
- [random-obj-prop](https://github.com/sindresorhus/random-obj-prop) - Get a random property from an object
|
|
113
|
+
- [unique-random](https://github.com/sindresorhus/unique-random) - Generate random numbers that are consecutively unique
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
<div align="center">
|
|
118
|
+
<b>
|
|
119
|
+
<a href="https://tidelift.com/subscription/pkg/npm-crypto-random-string?utm_source=npm-crypto-random-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
120
|
+
</b>
|
|
121
|
+
<br>
|
|
122
|
+
<sub>
|
|
123
|
+
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
124
|
+
</sub>
|
|
125
|
+
</div>
|