Skip to content

Commit 3c4424b

Browse files
Yanis Bensonsindresorhus
authored andcommitted
Fix a duplicate character in the urlSafeCharacters list (#10)
1 parent ab8ab11 commit 3c4424b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const crypto = require('crypto');
33

4-
const urlSafeCharacters = 'abcdefjhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.split('');
4+
const urlSafeCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.split('');
55

66
const generateForCustomCharacters = (length, characters) => {
77
// Generating entropy is faster than complex math operations, so we use the simplest way

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
11
import test from 'ava';
22
import cryptoRandomString from '.';
33

4+
// Probailistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656
5+
const generatedCharacterSetSize = (options, targetSize) => {
6+
const set = new Set();
7+
const length = targetSize * 640;
8+
const string = cryptoRandomString({...options, length});
9+
10+
for (let i = 0; i < length; i++) {
11+
set.add(string[i]);
12+
}
13+
14+
return set.size;
15+
};
16+
417
test('main', t => {
518
t.is(cryptoRandomString({length: 0}).length, 0);
619
t.is(cryptoRandomString({length: 10}).length, 10);
720
t.is(cryptoRandomString({length: 100}).length, 100);
821
t.regex(cryptoRandomString({length: 100}), /^[a-f\d]*$/); // Sanity check, probabilistic
22+
t.is(generatedCharacterSetSize({}, 16), 16);
923
});
1024

1125
test('hex', t => {
1226
t.is(cryptoRandomString({length: 0, type: 'hex'}).length, 0);
1327
t.is(cryptoRandomString({length: 10, type: 'hex'}).length, 10);
1428
t.is(cryptoRandomString({length: 100, type: 'hex'}).length, 100);
1529
t.regex(cryptoRandomString({length: 100, type: 'hex'}), /^[a-f\d]*$/); // Sanity check, probabilistic
30+
t.is(generatedCharacterSetSize({type: 'hex'}, 16), 16);
1631
});
1732

1833
test('base64', t => {
1934
t.is(cryptoRandomString({length: 0, type: 'base64'}).length, 0);
2035
t.is(cryptoRandomString({length: 10, type: 'base64'}).length, 10);
2136
t.is(cryptoRandomString({length: 100, type: 'base64'}).length, 100);
2237
t.regex(cryptoRandomString({length: 100, type: 'base64'}), /^[a-zA-Z\d/+]*$/); // Sanity check, probabilistic
38+
t.is(generatedCharacterSetSize({type: 'base64'}, 64), 64);
2339
});
2440

2541
test('url-safe', t => {
2642
t.is(cryptoRandomString({length: 0, type: 'url-safe'}).length, 0);
2743
t.is(cryptoRandomString({length: 10, type: 'url-safe'}).length, 10);
2844
t.is(cryptoRandomString({length: 100, type: 'url-safe'}).length, 100);
2945
t.regex(cryptoRandomString({length: 100, type: 'url-safe'}), /^[a-zA-Z\d._~-]*$/); // Sanity check, probabilistic
46+
t.is(generatedCharacterSetSize({type: 'url-safe'}, 66), 66);
3047
});
3148

3249
test('characters', t => {
3350
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
3451
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);
3552
t.is(cryptoRandomString({length: 100, characters: '1234'}).length, 100);
3653
t.regex(cryptoRandomString({length: 100, characters: '1234'}), /^[1-4]*$/); // Sanity check, probabilistic
54+
t.is(generatedCharacterSetSize({characters: '1234'}, 4), 4);
55+
t.is(generatedCharacterSetSize({characters: '0123456789'}, 10), 10);
3756
});
3857

3958
test('argument errors', t => {

0 commit comments

Comments
 (0)