Skip to content

Commit daa6f5a

Browse files
authored
Add ascii-printable character set (#26)
1 parent c409861 commit daa6f5a

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ interface TypeOption {
1717
1818
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.
1919
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+
2022
@example
2123
```
2224
cryptoRandomString({length: 10});
@@ -33,9 +35,12 @@ interface TypeOption {
3335
3436
cryptoRandomString({length: 6, type: 'distinguishable'});
3537
//=> 'CDEHKM'
38+
39+
cryptoRandomString({length: 10, type: 'ascii-printable'});
40+
//=> '`#Rt8$IK>B'
3641
```
3742
*/
38-
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable';
43+
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable';
3944
}
4045

4146
interface CharactersOption {

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const randomBytesAsync = promisify(crypto.randomBytes);
77
const urlSafeCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.split('');
88
const numericCharacters = '0123456789'.split('');
99
const distinguishableCharacters = 'CDEHKMPRTUWXY012458'.split('');
10+
const asciiPrintableCharacters = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.split('');
1011

1112
const generateForCustomCharacters = (length, characters) => {
1213
// Generating entropy is faster than complex math operations, so we use the simplest way
@@ -75,7 +76,8 @@ const allowedTypes = [
7576
'base64',
7677
'url-safe',
7778
'numeric',
78-
'distinguishable'
79+
'distinguishable',
80+
'ascii-printable'
7981
];
8082

8183
const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({length, type, characters}) => {
@@ -119,6 +121,10 @@ const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({
119121
return generateForCustomCharacters(length, distinguishableCharacters);
120122
}
121123

124+
if (type === 'ascii-printable') {
125+
return generateForCustomCharacters(length, asciiPrintableCharacters);
126+
}
127+
122128
if (characters.length === 0) {
123129
throw new TypeError('Expected `characters` string length to be greater than or equal to 1');
124130
}

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ cryptoRandomString({length: 10, type: 'numeric'});
3030
cryptoRandomString({length: 6, type: 'distinguishable'});
3131
//=> 'CDEHKM'
3232

33+
cryptoRandomString({length: 10, type: 'ascii-printable'});
34+
//=> '`#Rt8$IK>B'
35+
3336
cryptoRandomString({length: 10, characters: 'abc'});
3437
//=> 'abaaccabac'
3538
```
@@ -59,14 +62,16 @@ Length of the returned string.
5962

6063
Type: `string`\
6164
Default: `'hex'`\
62-
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable'`
65+
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable'`
6366

6467
Use only characters from a predefined set of allowed characters.
6568

6669
Cannot be set at the same time as the `characters` option.
6770

6871
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.
6972

73+
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.
74+
7075
##### characters
7176

7277
Type: `string`\

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ test('distinguishable', t => {
6969
t.is(generatedCharacterSetSize({type: 'distinguishable'}, 19), 19);
7070
});
7171

72+
test('ascii-printable', t => {
73+
t.is(cryptoRandomString({length: 0, type: 'ascii-printable'}).length, 0);
74+
t.is(cryptoRandomString({length: 10, type: 'ascii-printable'}).length, 10);
75+
t.is(cryptoRandomString({length: 100, type: 'ascii-printable'}).length, 100);
76+
t.regex(cryptoRandomString({length: 100, type: 'ascii-printable'}), /^[!"#$%&'()*+,-./\d:;<=>?@A-Z[\\\]^_`a-z{|}~]*$/); // Sanity check, probabilistic
77+
});
78+
7279
test('characters', t => {
7380
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
7481
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);

0 commit comments

Comments
 (0)