2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-01-10 11:27:15 -05:00
|
|
|
|
|
|
|
function getRandomValues(arrayBufferView) {
|
|
|
|
if (!ArrayBuffer.isView(arrayBufferView)) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Argument 1 does not implement interface ArrayBufferView",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
arrayBufferView instanceof Int8Array ||
|
|
|
|
arrayBufferView instanceof Uint8Array ||
|
|
|
|
arrayBufferView instanceof Int16Array ||
|
|
|
|
arrayBufferView instanceof Uint16Array ||
|
|
|
|
arrayBufferView instanceof Int32Array ||
|
|
|
|
arrayBufferView instanceof Uint32Array ||
|
|
|
|
arrayBufferView instanceof Uint8ClampedArray
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The provided ArrayBufferView is not an integer array type",
|
|
|
|
"TypeMismatchError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (arrayBufferView.byteLength > 65536) {
|
|
|
|
throw new DOMException(
|
|
|
|
`The ArrayBufferView's byte length (${arrayBufferView.byteLength}) exceeds the number of bytes of entropy available via this API (65536)`,
|
|
|
|
"QuotaExceededError",
|
|
|
|
);
|
2020-11-13 16:01:57 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
const ui8 = new Uint8Array(
|
2021-01-10 11:27:15 -05:00
|
|
|
arrayBufferView.buffer,
|
|
|
|
arrayBufferView.byteOffset,
|
|
|
|
arrayBufferView.byteLength,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 19:24:38 -05:00
|
|
|
core.jsonOpSync("op_crypto_get_random_values", {}, ui8);
|
2021-01-10 11:27:15 -05:00
|
|
|
return arrayBufferView;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-10 11:27:15 -05:00
|
|
|
|
2020-11-13 16:01:57 -05:00
|
|
|
window.crypto = {
|
|
|
|
getRandomValues,
|
|
|
|
};
|
|
|
|
window.__bootstrap = window.__bootstrap || {};
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.crypto = {
|
|
|
|
getRandomValues,
|
|
|
|
};
|
|
|
|
})(this);
|