2021-01-12 02:13:41 +09:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-05 03:48:32 +05:30
|
|
|
"use strict";
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 22:22:43 +02:00
|
|
|
const core = window.Deno.core;
|
2021-01-10 17:27:15 +01: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-14 02:31:57 +05:30
|
|
|
}
|
2020-07-19 19:49:44 +02:00
|
|
|
const ui8 = new Uint8Array(
|
2021-01-10 17:27:15 +01:00
|
|
|
arrayBufferView.buffer,
|
|
|
|
arrayBufferView.byteOffset,
|
|
|
|
arrayBufferView.byteLength,
|
2020-07-19 19:49:44 +02:00
|
|
|
);
|
2021-01-15 01:24:38 +01:00
|
|
|
core.jsonOpSync("op_crypto_get_random_values", {}, ui8);
|
2021-01-10 17:27:15 +01:00
|
|
|
return arrayBufferView;
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
2021-01-10 17:27:15 +01:00
|
|
|
|
2020-11-14 02:31:57 +05:30
|
|
|
window.crypto = {
|
|
|
|
getRandomValues,
|
|
|
|
};
|
|
|
|
window.__bootstrap = window.__bootstrap || {};
|
2020-07-19 19:49:44 +02:00
|
|
|
window.__bootstrap.crypto = {
|
|
|
|
getRandomValues,
|
|
|
|
};
|
|
|
|
})(this);
|