Skip to content

Commit ef0dd2c

Browse files
committedAug 14, 2017
Examples
From README
1 parent 2e1e344 commit ef0dd2c

29 files changed

+360
-180
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
*~
33
.DS_Store
4+
examples/dist

‎README.md

Lines changed: 155 additions & 112 deletions
Large diffs are not rendered by default.

‎dist/lib/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var _stringWithBytes = function _stringWithBytes(entropyBits, bytes, charSet) {
126126

127127
var needed = Math.ceil(count * (bitsPerChar / _bitsPerByte));
128128
if (bytes.length < needed) {
129-
throw new Error('Insufficient bytes. Need ' + needed);
129+
throw new Error('Insufficient bytes: need ' + needed + ' and got ' + bytes.length);
130130
}
131131

132132
var charsPerChunk = charSet.getCharsPerChunk();

‎entropy-string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Random = require('./dist/lib/random').default
22
const Entropy = require('./dist/lib/entropy').default
33
const CharSet = require('./dist/lib/charSet').default
4-
const {charSet2, charSet4, charSet8, charSet16, charSet32, charSet64} = require('./dist/lib/random')
4+
const {charSet2, charSet4, charSet8, charSet16, charSet32, charSet64} = require('./dist/lib/charSet')
55

66
module.exports = {
77
Random,

‎examples.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎examples/charSets.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {charSet64, charSet32, charSet16, charSet8, charSet4, charSet2} from './entropy-string'
2+
3+
console.log('\n charSet64: ' + charSet64.chars())
4+
console.log('\n charSet32: ' + charSet32.chars())
5+
console.log('\n charSet16: ' + charSet16.chars())
6+
console.log('\n charSet8: ' + charSet8.chars())
7+
console.log('\n charSet4: ' + charSet4.chars())
8+
console.log('\n charSet2: ' + charSet2.chars() + '\n')

‎examples/custom_bytes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random()
4+
let bytes = Buffer.from([250, 200, 150, 100])
5+
let string = random.stringWithBytes(30, bytes)
6+
console.log('\n Custom bytes string : ' + string + '\n')
7+
8+
try {
9+
string = random.stringWithBytes(32, bytes)
10+
}
11+
catch(error) {
12+
console.log(' Error: ' + error.message)
13+
}
14+

‎examples/custom_chars_1.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Random, charSet2} from './entropy-string'
2+
3+
let random = new Random(charSet2)
4+
let flips = random.string(10)
5+
console.log('\n 10 flips: ' + flips + '\n')
6+
7+
random.useChars('HT')
8+
flips = random.string(10)
9+
console.log('\n 10 flips: ' + flips + '\n')
10+

‎examples/custom_chars_2.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random('0123456789ABCDEF')
4+
let string = random.string(48)
5+
console.log('\n Uppercase hex: ' + string + '\n')

‎examples/custom_chars_3.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Random} from './entropy-string'
2+
3+
try {
4+
let random = new Random('123456')
5+
}
6+
catch(error) {
7+
console.log('Error: ' + error.message)
8+
}
9+
10+
11+
try {
12+
let random = new Random('01233210')
13+
}
14+
catch(error) {
15+
console.log('Error: ' + error.message)
16+
}

‎examples/efficiency.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random()
4+
let string = random.string(80)
5+
console.log('\n CSPRNG base 32 80-bit string : ' + string)
6+
7+
string = random.stringRandom(80)
8+
console.log('\n PRNG base 32 80-bit string : ' + string + '\n')
9+

‎examples/entropy-string.js

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/gen5.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Random, Entropy, charSet16} from './entropy-string'
2+
3+
let bits = Entropy.bits(10000, 1000000)
4+
let random = new Random(charSet16)
5+
let strings = Array()
6+
for (let i = 0; i < 5; i++) {
7+
let string = random.string(bits)
8+
strings.push(string)
9+
}
10+
console.log('\n 5 IDs: ' + strings.join(', ') + '\n')

‎examples/more_1.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Random, Entropy} from './entropy-string'
2+
3+
let bits = Entropy.bits(10000, 1000000)
4+
let random = new Random()
5+
let string = random.string(bits)
6+
console.log('\n Base 32 string : ' + string + '\n')
7+

‎examples/more_2.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Random, Entropy, charSet16, charSet4} from './entropy-string'
2+
3+
let bits = Entropy.bits(30, 100000)
4+
let random = new Random(charSet16)
5+
let string = random.string(bits)
6+
console.log('\n Base 16 string : ' + string)
7+
8+
random.use(charSet4)
9+
string = random.string(bits)
10+
console.log('\n Base 4 string : ' + string + '\n')

‎examples/more_3.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Random, Entropy} from './entropy-string'
2+
3+
let bits = Entropy.bitsWithPowers(10, 12)
4+
let random = new Random()
5+
let string = random.string(bits)
6+
console.log('\n Base 32 string : ' + string + '\n')

‎examples/more_4.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random()
4+
let string = random.string(128)
5+
console.log('\n Base 32 session ID: ' + string + '\n')

‎examples/more_5.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random, charSet64} from './entropy-string'
2+
3+
let random = new Random(charSet64)
4+
let string = random.sessionID()
5+
console.log('\n Base 64 session ID: ' + string + '\n')

‎examples/tldr.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Random, Entropy, charSet16, charSet32, charSet64} from './entropy-string'
2+
3+
var print = console.log
4+
5+
var random = new Random()
6+
7+
print('\nOWASP session ID using base32 characters: ' + random.sessionID())
8+
9+
print('\nSession ID using file system and URL safe characters: ' + random.sessionID(charSet64))
10+
11+
print('\n192-bit entropy string using base32 characters: ' + random.string(192))
12+
13+
random = new Random(charSet16)
14+
print('\n48-bit entropy string using hex characters: ' + random.string(48))
15+
16+
random.useChars('1234567890ABCDEF')
17+
print('\n48-bit entropy string using uppercase hex characters: ' + random.string(48))
18+
19+
random.use(charSet32)
20+
var bits = Entropy.bits(30, 1000000)
21+
print('\nBase 32 string with a 1 in a million chance of a repeat in 30 strings: ' +
22+
random.string(bits))
23+
24+
bits = Entropy.bitsWithPowers(8, 12)
25+
print('\nBase 64 string with a 1 in a trillion chance of a repeat in 100 million strings: ' +
26+
random.string(bits, charSet64))
27+
28+
print('');

‎examples/tldr2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Random, Entropy} from './entropy-string'
2+
3+
let bits = Entropy.bitsWithPowers(6,9)
4+
let random = new Random()
5+
let string = random.string(bits)
6+
console.log('\n Base32 string with a 1 in a billion chance of repeat in a million strings: ' + string + '\n')

‎examples/tldr_1.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random()
4+
let string = random.sessionID()
5+
console.log('\n OWASP session ID using base32 characters: ' + string + '\n')

‎examples/tldr_2.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random, charSet64} from './entropy-string'
2+
3+
let random = new Random()
4+
let string = random.sessionID(charSet64)
5+
console.log('\n OWASP session ID using RFC 4648 file system and URL safe characters: ' + string + '\n')

‎examples/tldr_3.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random, charSet16} from './entropy-string'
2+
3+
let random = new Random(charSet16)
4+
let string = random.string(48)
5+
console.log('\n 48-bit string using hex characters: ' + string + '\n')

‎examples/tldr_4.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Random} from './entropy-string'
2+
3+
let random = new Random('0123456789ABCDEF')
4+
let string = random.string(48)
5+
console.log('\n 48-bit string using hex characters: ' + string + '\n')

‎examples/tldr_5.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Random, Entropy} from './entropy-string'
2+
3+
let bits = Entropy.bits(30, 1000000)
4+
let random = new Random()
5+
let string = random.string(bits)
6+
console.log('\n Base 32 character string with a 1 in a million chance of a repeat in 30 strings: ' + string + '\n')

‎examples/tldr_6.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Random, Entropy, charSet64} from './entropy-string'
2+
3+
let bits = Entropy.bitsWithPowers(7, 12)
4+
let random = new Random(charSet64)
5+
let string = random.string(bits)
6+
console.log('\n Base 64 character string with a 1 in a trillion chance of a repeat in 100 million strings: ' + string + '\n')

‎lib/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const _stringWithBytes = (entropyBits, bytes, charSet) => {
7474

7575
const needed = Math.ceil(count * (bitsPerChar / _bitsPerByte))
7676
if (bytes.length < needed) {
77-
throw new Error('Insufficient bytes. Need ' + needed)
77+
throw new Error('Insufficient bytes: need ' + needed + ' and got ' + bytes.length)
7878
}
7979

8080
const charsPerChunk = charSet.getCharsPerChunk()

‎package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"build": "BABEL_ENV=production babel --out-dir=dist lib/*.js",
13-
"examples": "BABEL_ENV=development babel --out-dir=tmp examples.js",
13+
"examples": "yarn build && cd examples && BABEL_ENV=development babel --out-dir=dist *.js && cd ..",
1414
"prepare": "yarn build",
1515
"test": "ava"
1616
},
@@ -21,7 +21,9 @@
2121
"keywords": [
2222
"entropy",
2323
"random",
24-
"string"
24+
"string",
25+
"secure",
26+
"security"
2527
],
2628
"author": "Paul Rogers <http://knoxen.com>",
2729
"maintainers": [

‎tmp/examples.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)