-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrandom-openssl.js
More file actions
145 lines (110 loc) · 2.64 KB
/
random-openssl.js
File metadata and controls
145 lines (110 loc) · 2.64 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
* random-openssl.js - random number generator for bcrypto
* Copyright (c) 2014-2020, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*
* Resources:
* https://wiki.openssl.org/index.php/Random_Numbers
* https://csrc.nist.gov/projects/random-bit-generation/
* http://www.pcg-random.org/posts/bounded-rands.html
*/
'use strict';
const assert = require('../internal/assert');
const crypto = require('crypto');
// See: https://github.com/nodejs/node/issues/31442
const randomFillSync = crypto.randomFillSync.bind(crypto);
const pool = new Uint32Array(16);
let poolPos = 0;
/**
* Generate pseudo-random bytes.
* @param {Number} size
* @returns {Buffer}
*/
function randomBytes(size) {
assert((size >>> 0) === size);
const data = Buffer.alloc(size);
randomFillSync(data, 0, size);
return data;
}
/**
* Generate pseudo-random bytes.
* @param {Buffer} data
* @param {Number} [off=0]
* @param {Number} [size=data.length-off]
* @returns {Buffer}
*/
function randomFill(data, off, size) {
assert(Buffer.isBuffer(data));
if (off == null)
off = 0;
assert((off >>> 0) === off);
if (size == null)
size = data.length - off;
assert((size >>> 0) === size);
assert(off + size <= data.length);
randomFillSync(data, off, size);
return data;
}
/**
* Generate a random uint32.
* @returns {Number}
*/
function randomInt() {
if ((poolPos & 15) === 0) {
getRandomValues(pool);
poolPos = 0;
}
return pool[poolPos++];
}
/**
* Generate a random uint32 within a range.
* @param {Number} min - Inclusive.
* @param {Number} max - Exclusive.
* @returns {Number}
*/
function randomRange(min, max) {
assert((min >>> 0) === min);
assert((max >>> 0) === max);
assert(max >= min);
const space = max - min;
if (space === 0)
return min;
const top = -space >>> 0;
let x, r;
do {
x = randomInt();
r = x % space;
} while (x - r > top);
return r + min;
}
/*
* Helpers
*/
let hasTypedArray = null;
function getRandomValues(array) {
assert(array != null && typeof array === 'object');
assert(array.buffer instanceof ArrayBuffer);
if (hasTypedArray === null) {
try {
// Added in 9.0.0.
randomFillSync(new Uint32Array(1));
hasTypedArray = true;
} catch (e) {
hasTypedArray = false;
}
}
if (!hasTypedArray) {
array = Buffer.from(array.buffer,
array.byteOffset,
array.byteLength);
}
randomFillSync(array);
}
/*
* Expose
*/
exports.native = 1;
exports.randomBytes = randomBytes;
exports.randomFill = randomFill;
exports.randomInt = randomInt;
exports.randomRange = randomRange;