-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (59 loc) · 1.55 KB
/
index.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
import { NativeModules } from 'react-native'
import { Buffer } from 'buffer'
const { SecureRandom } = NativeModules
const DESIRED_POOL_SIZE = 2048
const MINIMUM_POOL_SIZE = 1024
const pool = []
function b64ToUInt8(b64) {
return new Uint8Array(Buffer(b64, 'base64'))
}
/**
* Refill the random pool used by randomBytes()
*
* Normally you do not need to call this function yourself; it is
* automatically invoked by randomBytesSync if the pool is starting
* to become low.
*
* @returns {Promise<void>}
*/
export async function fillPool() {
const randomAvailable = pool.length
if (randomAvailable < MINIMUM_POOL_SIZE) {
const seed = await randomBytesAsync(DESIRED_POOL_SIZE - randomAvailable)
pool.push(...seed)
}
}
/**
* Generate random data asynchronously
*
* @param {number} length Number of bytes to generate
* @returns {Promise<Uint8Array>}
*/
export function randomBytesAsync(length) {
return SecureRandom.randomBytesAsync(length).then(b64ToUInt8)
}
/**
* Generate random data synchronously.
*
* @param {number} length Number of bytes to generate
* @returns {number[]}
*/
export function randomBytesSync(length) {
if (length > pool.length) {
throw new Error('Random pool depleted')
}
const bytes = pool.splice(0, length)
fillPool()
return bytes
}
/**
* Alias randomBytes to randomBytesSync, so that
* the module can work as a replacement to node's crypto.
*
* @param {number} length Number of bytes to generate
* @returns {number[]}
*/
export function randomBytes(length) {
return randomBytesSync(length)
}
fillPool()