mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
Test crypto.getRandomValues() with wpt (#9016)
This commit is contained in:
parent
cd2f7ae69d
commit
f7e09c6a55
2 changed files with 36 additions and 8 deletions
|
@ -173,5 +173,8 @@
|
|||
"measure-l3",
|
||||
"structured-serialize-detail",
|
||||
"user_timing_exists"
|
||||
],
|
||||
"WebCryptoApi": [
|
||||
"getRandomValues"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -2,19 +2,44 @@
|
|||
|
||||
((window) => {
|
||||
const core = window.Deno.core;
|
||||
function getRandomValues(typedArray) {
|
||||
if (typedArray == null) throw new Error("Input must not be null");
|
||||
if (typedArray.length > 65536) {
|
||||
throw new Error("Input must not be longer than 65536");
|
||||
|
||||
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",
|
||||
);
|
||||
}
|
||||
const ui8 = new Uint8Array(
|
||||
typedArray.buffer,
|
||||
typedArray.byteOffset,
|
||||
typedArray.byteLength,
|
||||
arrayBufferView.buffer,
|
||||
arrayBufferView.byteOffset,
|
||||
arrayBufferView.byteLength,
|
||||
);
|
||||
core.jsonOpSync("op_get_random_values", {}, ui8);
|
||||
return typedArray;
|
||||
return arrayBufferView;
|
||||
}
|
||||
|
||||
window.crypto = {
|
||||
getRandomValues,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue