summaryrefslogtreecommitdiff
path: root/op_crates/crypto/01_crypto.js
diff options
context:
space:
mode:
authorYacine Hmito <yacinehmito@users.noreply.github.com>2021-01-10 17:27:15 +0100
committerGitHub <noreply@github.com>2021-01-10 11:27:15 -0500
commitf7e09c6a555e115c004feca56f8b279c5f18c9b2 (patch)
tree7113675eedf3bab8d1344d4c34898aab191506aa /op_crates/crypto/01_crypto.js
parentcd2f7ae69d85c56d4865423f6f76d6911f7fe8a6 (diff)
Test crypto.getRandomValues() with wpt (#9016)
Diffstat (limited to 'op_crates/crypto/01_crypto.js')
-rw-r--r--op_crates/crypto/01_crypto.js41
1 files changed, 33 insertions, 8 deletions
diff --git a/op_crates/crypto/01_crypto.js b/op_crates/crypto/01_crypto.js
index 2f9424492..d0b0d98c7 100644
--- a/op_crates/crypto/01_crypto.js
+++ b/op_crates/crypto/01_crypto.js
@@ -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,
};