summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Crypto/Crypto.cpp
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-09-30 20:02:55 +0300
committerAndreas Kling <kling@serenityos.org>2021-09-30 20:02:09 +0200
commit2c6c9b73c88bd2feea66df8766212c2e586ea9aa (patch)
treeb1e0712844874577ac463a506e9164e93b93493f /Userland/Libraries/LibWeb/Crypto/Crypto.cpp
parentbecbb0ea97bf2da194246f1dde1d2b8fba16946d (diff)
downloadserenity-2c6c9b73c88bd2feea66df8766212c2e586ea9aa.zip
LibWeb: Add the Web::Crypto namespace, built-in, and getRandomValues
Since we don't support IDL typedefs or unions yet, the responsibility of verifying the type of the argument is temporarily moved from the generated Wrapper to the implementation.
Diffstat (limited to 'Userland/Libraries/LibWeb/Crypto/Crypto.cpp')
-rw-r--r--Userland/Libraries/LibWeb/Crypto/Crypto.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp
new file mode 100644
index 0000000000..64ba54f6ed
--- /dev/null
+++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Random.h>
+#include <LibJS/Runtime/TypedArray.h>
+#include <LibWeb/Bindings/Wrapper.h>
+#include <LibWeb/Crypto/Crypto.h>
+
+namespace Web::Crypto {
+
+DOM::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const
+{
+ // 1. If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm.
+ if (!array.is_object() || !(is<JS::Int8Array>(array.as_object()) || is<JS::Uint8Array>(array.as_object()) || is<JS::Uint8ClampedArray>(array.as_object()) || is<JS::Int16Array>(array.as_object()) || is<JS::Uint16Array>(array.as_object()) || is<JS::Int32Array>(array.as_object()) || is<JS::Uint32Array>(array.as_object()) || is<JS::BigInt64Array>(array.as_object()) || is<JS::BigUint64Array>(array.as_object())))
+ return DOM::TypeMismatchError::create("array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array");
+ auto& typed_array = static_cast<JS::TypedArrayBase&>(array.as_object());
+
+ // 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm.
+ if (typed_array.byte_length() > 65536)
+ return DOM::QuotaExceededError::create("array's byteLength may not be greater than 65536");
+
+ // IMPLEMENTATION DEFINED: If the viewed array buffer is detached, throw a InvalidStateError and terminate the algorithm.
+ if (typed_array.viewed_array_buffer()->is_detached())
+ return DOM::InvalidStateError::create("array is detached");
+ // FIXME: Handle SharedArrayBuffers
+
+ // 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type.
+ fill_with_random(typed_array.viewed_array_buffer()->buffer().data(), typed_array.viewed_array_buffer()->buffer().size());
+
+ // 4. Return array.
+ return array;
+}
+
+}