-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser.js
63 lines (61 loc) · 1.72 KB
/
browser.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
const MAX_SIZE = 65536
let base
let getRandomValues
let polyfill
if (window.msCrypto || !!window.StyleMedia) { // IE or Metro
base = window.msCrypto || window.crypto
const randomValues = base.getRandomValues
getRandomValues = function (input) {
if (input instanceof Uint8ClampedArray) {
randomValues.call(base, new Uint8Array(input.buffer, input.byteOffset, input.byteLength))
} else {
randomValues.call(base, input)
}
return input
}
polyfill = function () {
if (!window.crypto) {
window.crypto = {}
}
if (window.crypto.getRandomValues !== getRandomValues) {
window.crypto._getRandomValues = window.crypto.getRandomValues
window.crypto.getRandomValues = getRandomValues
}
}
} else {
base = window.crypto
polyfill = function () {}
getRandomValues = base.getRandomValues
}
function getRandomValuesBrowser (input) {
const n = input.byteLength
const offset = input.byteOffset
const buffer = input.buffer
if (n <= MAX_SIZE) {
getRandomValues.call(
base,
(
input instanceof Float32Array ||
input instanceof Float64Array ||
input instanceof DataView ||
(
window.BigInt64Array &&
(
input instanceof window.BigInt64Array ||
input instanceof window.BigUint64Array
)
)
)
? new Uint8Array(buffer, offset, n)
: input
)
} else {
for (let i = 0; i < n; i += MAX_SIZE) {
getRandomValues.call(base, new Uint8Array(buffer, i + offset, Math.min(n - i, MAX_SIZE)))
}
}
return input
}
getRandomValuesBrowser.name = 'getRandomValuesBrowser' // IE 11
getRandomValuesBrowser.polyfill = polyfill
module.exports = getRandomValuesBrowser