browser.js
2.93 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
'use strict'
function oldBrowser () {
throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11')
}
var safeBuffer = require('safe-buffer')
var randombytes = require('randombytes')
var Buffer = safeBuffer.Buffer
var kBufferMaxLength = safeBuffer.kMaxLength
var crypto = global.crypto || global.msCrypto
var kMaxUint32 = Math.pow(2, 32) - 1
function assertOffset (offset, length) {
if (typeof offset !== 'number' || offset !== offset) { // eslint-disable-line no-self-compare
throw new TypeError('offset must be a number')
}
if (offset > kMaxUint32 || offset < 0) {
throw new TypeError('offset must be a uint32')
}
if (offset > kBufferMaxLength || offset > length) {
throw new RangeError('offset out of range')
}
}
function assertSize (size, offset, length) {
if (typeof size !== 'number' || size !== size) { // eslint-disable-line no-self-compare
throw new TypeError('size must be a number')
}
if (size > kMaxUint32 || size < 0) {
throw new TypeError('size must be a uint32')
}
if (size + offset > length || size > kBufferMaxLength) {
throw new RangeError('buffer too small')
}
}
if ((crypto && crypto.getRandomValues) || !process.browser) {
exports.randomFill = randomFill
exports.randomFillSync = randomFillSync
} else {
exports.randomFill = oldBrowser
exports.randomFillSync = oldBrowser
}
function randomFill (buf, offset, size, cb) {
if (!Buffer.isBuffer(buf) && !(buf instanceof global.Uint8Array)) {
throw new TypeError('"buf" argument must be a Buffer or Uint8Array')
}
if (typeof offset === 'function') {
cb = offset
offset = 0
size = buf.length
} else if (typeof size === 'function') {
cb = size
size = buf.length - offset
} else if (typeof cb !== 'function') {
throw new TypeError('"cb" argument must be a function')
}
assertOffset(offset, buf.length)
assertSize(size, offset, buf.length)
return actualFill(buf, offset, size, cb)
}
function actualFill (buf, offset, size, cb) {
if (process.browser) {
var ourBuf = buf.buffer
var uint = new Uint8Array(ourBuf, offset, size)
crypto.getRandomValues(uint)
if (cb) {
process.nextTick(function () {
cb(null, buf)
})
return
}
return buf
}
if (cb) {
randombytes(size, function (err, bytes) {
if (err) {
return cb(err)
}
bytes.copy(buf, offset)
cb(null, buf)
})
return
}
var bytes = randombytes(size)
bytes.copy(buf, offset)
return buf
}
function randomFillSync (buf, offset, size) {
if (typeof offset === 'undefined') {
offset = 0
}
if (!Buffer.isBuffer(buf) && !(buf instanceof global.Uint8Array)) {
throw new TypeError('"buf" argument must be a Buffer or Uint8Array')
}
assertOffset(offset, buf.length)
if (size === undefined) size = buf.length - offset
assertSize(size, offset, buf.length)
return actualFill(buf, offset, size)
}