0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

Test crypto.getRandomValues() with wpt (#9016)

This commit is contained in:
Yacine Hmito 2021-01-10 17:27:15 +01:00 committed by GitHub
parent cd2f7ae69d
commit f7e09c6a55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View file

@ -173,5 +173,8 @@
"measure-l3", "measure-l3",
"structured-serialize-detail", "structured-serialize-detail",
"user_timing_exists" "user_timing_exists"
],
"WebCryptoApi": [
"getRandomValues"
] ]
} }

View file

@ -2,19 +2,44 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
function getRandomValues(typedArray) {
if (typedArray == null) throw new Error("Input must not be null"); function getRandomValues(arrayBufferView) {
if (typedArray.length > 65536) { if (!ArrayBuffer.isView(arrayBufferView)) {
throw new Error("Input must not be longer than 65536"); 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",
);
} }
const ui8 = new Uint8Array( const ui8 = new Uint8Array(
typedArray.buffer, arrayBufferView.buffer,
typedArray.byteOffset, arrayBufferView.byteOffset,
typedArray.byteLength, arrayBufferView.byteLength,
); );
core.jsonOpSync("op_get_random_values", {}, ui8); core.jsonOpSync("op_get_random_values", {}, ui8);
return typedArray; return arrayBufferView;
} }
window.crypto = { window.crypto = {
getRandomValues, getRandomValues,
}; };