diff options
author | stelar7 <dudedbz@gmail.com> | 2022-03-30 15:58:02 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-30 18:47:53 +0200 |
commit | b526a10d767d1257fe44b2ae0e0e08a7da0ca2e5 (patch) | |
tree | f2ebcc72d24aeb452a72f13617dabff192f76658 /Userland | |
parent | 9a60b697aac6355c11fc86e9db0b8ad61a2a96e9 (diff) | |
download | serenity-b526a10d767d1257fe44b2ae0e0e08a7da0ca2e5.zip |
LibWeb: Add Crypto.randomUUID()
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.cpp | 55 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.idl | 2 |
3 files changed, 58 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp index 020f10c0d8..277fec25b5 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp @@ -1,10 +1,12 @@ /* * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> + * Copyright (c) 2022, stelar7 <dudedbz@gmail.com> * * SPDX-License-Identifier: BSD-2-Clause */ #include <AK/Random.h> +#include <AK/StringBuilder.h> #include <LibJS/Runtime/TypedArray.h> #include <LibWeb/Bindings/Wrapper.h> #include <LibWeb/Crypto/Crypto.h> @@ -17,6 +19,7 @@ Crypto::Crypto() { } +// https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues 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. @@ -40,4 +43,56 @@ DOM::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const return array; } +// https://w3c.github.io/webcrypto/#dfn-Crypto-method-randomUUID +String Crypto::random_uuid() const +{ + // 1. Let bytes be a byte sequence of length 16. + u8 bytes[16]; + + // 2. Fill bytes with cryptographically secure random bytes. + fill_with_random(bytes, 16); + + // 3. Set the 4 most significant bits of bytes[6], which represent the UUID version, to 0100. + bytes[6] &= ~(1 << 7); + bytes[6] |= 1 << 6; + bytes[6] &= ~(1 << 5); + bytes[6] &= ~(1 << 4); + + // 4. Set the 2 most significant bits of bytes[8], which represent the UUID variant, to 10. + bytes[8] |= 1 << 7; + bytes[8] &= ~(1 << 6); + + /* 5. Return the string concatenation of + ยซ + hexadecimal representation of bytes[0], + hexadecimal representation of bytes[1], + hexadecimal representation of bytes[2], + hexadecimal representation of bytes[3], + "-", + hexadecimal representation of bytes[4], + hexadecimal representation of bytes[5], + "-", + hexadecimal representation of bytes[6], + hexadecimal representation of bytes[7], + "-", + hexadecimal representation of bytes[8], + hexadecimal representation of bytes[9], + "-", + hexadecimal representation of bytes[10], + hexadecimal representation of bytes[11], + hexadecimal representation of bytes[12], + hexadecimal representation of bytes[13], + hexadecimal representation of bytes[14], + hexadecimal representation of bytes[15] + ยป. + */ + StringBuilder builder; + builder.appendff("{:02x}{:02x}{:02x}{:02x}-", bytes[0], bytes[1], bytes[2], bytes[3]); + builder.appendff("{:02x}{:02x}-", bytes[4], bytes[5]); + builder.appendff("{:02x}{:02x}-", bytes[6], bytes[7]); + builder.appendff("{:02x}{:02x}-", bytes[8], bytes[9]); + builder.appendff("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]); + return builder.to_string(); +} + } diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.h b/Userland/Libraries/LibWeb/Crypto/Crypto.h index 172e7de4e8..59dfeb4087 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.h +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.h @@ -27,6 +27,7 @@ public: NonnullRefPtr<SubtleCrypto> subtle() const { return m_subtle; } DOM::ExceptionOr<JS::Value> get_random_values(JS::Value array) const; + String random_uuid() const; private: Crypto(); diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.idl b/Userland/Libraries/LibWeb/Crypto/Crypto.idl index 0cfee5afeb..fbf28fcd20 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.idl +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.idl @@ -6,4 +6,6 @@ interface Crypto { // FIXME: the argument and the return value should be of type ArrayBufferView any getRandomValues(any array); + + [SecureContext] DOMString randomUUID(); }; |