-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathrandombytes.js
110 lines (87 loc) · 2.71 KB
/
randombytes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const test = require('brittle')
const sodium = require('..')
test('constants', function (t) {
t.alike(typeof sodium.randombytes_SEEDBYTES, 'number', 'randombytes_SEEDBYTES is number')
})
test('randombytes_random', function (t) {
for (let i = 0; i < 1e6; i++) {
const n = sodium.randombytes_random()
if (n > 0xffffffff || n < 0) t.fail()
}
})
test('randombytes_uniform', function (t) {
const p = 5381
for (let i = 0; i < 1e6; i++) {
const n = sodium.randombytes_uniform(5381)
if (n >= p || n < 0) t.fail()
}
})
test('randombytes_buf', function (t) {
let buf = null
buf = Buffer.alloc(10)
sodium.randombytes_buf(buf)
t.not(buf, Buffer.alloc(10), 'not blank')
buf = Buffer.alloc(1024)
sodium.randombytes_buf(buf)
t.not(buf, Buffer.alloc(1024), 'large not blank')
})
test('randombytes_deterministic', function (t) {
const seed1 = Buffer.allocUnsafe(sodium.randombytes_SEEDBYTES)
const seed2 = Buffer.allocUnsafe(sodium.randombytes_SEEDBYTES)
const buf1 = Buffer.alloc(10)
const buf2 = Buffer.alloc(10)
for (let i = 0; i < 1e6; i++) {
sodium.randombytes_buf(seed1)
sodium.randombytes_buf(seed2)
sodium.randombytes_buf_deterministic(buf1, seed1)
sodium.randombytes_buf_deterministic(buf2, seed1)
if (!buf1.equals(buf2)) t.fail('should equal')
sodium.randombytes_buf_deterministic(buf1, seed1)
sodium.randombytes_buf_deterministic(buf2, seed2)
if (buf1.equals(buf2)) t.fail('should not equal')
sodium.randombytes_buf_deterministic(buf1, seed2)
sodium.randombytes_buf_deterministic(buf2, seed1)
if (buf1.equals(buf2)) t.fail('should not equal')
sodium.randombytes_buf_deterministic(buf1, seed2)
sodium.randombytes_buf_deterministic(buf2, seed2)
if (!buf1.equals(buf2)) t.fail('should equal')
}
})
test.skip('Various test cases', function (t) {
sodium.randombytes_buf(Buffer.alloc(0))
sodium.randombytes_buf(new Uint8Array(16))
t.throws(function () {
sodium.randombytes_buf([])
})
t.end()
})
test('Generates random bytes', function (t) {
const bufConst = Buffer.alloc(64)
sodium.randombytes_buf(bufConst)
const buf1 = Buffer.alloc(64)
for (let i = 0; i < 1e4; i++) {
sodium.randombytes_buf(buf1)
if (Buffer.compare(buf1, bufConst) === 0) {
t.fail('Constant buffer should not be equal')
t.end()
return
}
}
t.pass('Generated unique buffers')
t.end()
})
test('Exceed quota', function (t) {
const buf = Buffer.alloc(1 << 17)
sodium.randombytes_buf(buf)
const scores = new Array(256)
scores.fill(0)
for (const b of buf) {
scores[b]++
}
scores
.map(cnt => cnt / 256)
.forEach(cnt => {
if (cnt < 1 && cnt > 3) t.fail('Statistically unreasonable')
})
t.end()
})