Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ascii-printable character set #26

Merged
merged 8 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MergeExclusive} from 'type-fest';
import { MergeExclusive } from 'type-fest';
Richienb marked this conversation as resolved.
Show resolved Hide resolved

interface BaseOptions {
/**
Expand Down Expand Up @@ -35,7 +35,7 @@ interface TypeOption {
//=> 'CDEHKM'
```
*/
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable';
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable';
Richienb marked this conversation as resolved.
Show resolved Hide resolved
}

interface CharactersOption {
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const crypto = require('crypto');
const urlSafeCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.split('');
const numericCharacters = '0123456789'.split('');
const distinguishableCharacters = 'CDEHKMPRTUWXY012458'.split('');
const asciiPrintableCharacters = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.split('');
Richienb marked this conversation as resolved.
Show resolved Hide resolved

const generateForCustomCharacters = (length, characters) => {
// Generating entropy is faster than complex math operations, so we use the simplest way
Expand Down Expand Up @@ -38,7 +39,8 @@ const allowedTypes = [
'base64',
'url-safe',
'numeric',
'distinguishable'
'distinguishable',
'ascii-printable'
];

module.exports = ({length, type, characters}) => {
Expand Down Expand Up @@ -82,6 +84,10 @@ module.exports = ({length, type, characters}) => {
return generateForCustomCharacters(length, distinguishableCharacters);
}

if (type === 'ascii-printable') {
return generateForCustomCharacters(length, asciiPrintableCharacters);
}

if (characters.length === 0) {
throw new TypeError('Expected `characters` string length to be greater than or equal to 1');
}
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ Length of the returned string.

Type: `string`\
Default: `'hex'`\
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable'`
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable'`

Use only characters from a predefined set of allowed characters.

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

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.

The `ascii-printable` set contains all [printable ascii characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): `` !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~``

##### characters

Type: `string`\
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ test('distinquishable', t => {
t.is(generatedCharacterSetSize({type: 'distinguishable'}, 19), 19);
});

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

test('characters', t => {
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);
Expand Down