diff options
author | Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com> | 2023-03-13 16:22:07 +0300 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-03-16 13:17:37 -0400 |
commit | e8550ed21dd108888bb32d9206f51d577fd022bc (patch) | |
tree | 435102efde557ff18f162b18dd7c35838c9debd6 /Userland | |
parent | 12cd74495acc0e8ca0516406c29b4ace45d62133 (diff) | |
download | serenity-e8550ed21dd108888bb32d9206f51d577fd022bc.zip |
LibWeb: Move code that generates uuid into separate function
Make possible to generate uuid without having crypto class instance.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.h | 2 |
2 files changed, 22 insertions, 13 deletions
diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp index 139c6bf2ed..df08060f9c 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp @@ -72,6 +72,18 @@ WebIDL::ExceptionOr<String> Crypto::random_uuid() const { auto& vm = realm().vm(); + return TRY_OR_THROW_OOM(vm, generate_random_uuid()); +} + +void Crypto::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_subtle.ptr()); +} + +// https://w3c.github.io/webcrypto/#dfn-generate-a-random-uuid +ErrorOr<String> generate_random_uuid() +{ // 1. Let bytes be a byte sequence of length 16. u8 bytes[16]; @@ -113,18 +125,13 @@ WebIDL::ExceptionOr<String> Crypto::random_uuid() const ยป. */ StringBuilder builder; - TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}{:02x}{:02x}-", bytes[0], bytes[1], bytes[2], bytes[3])); - TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[4], bytes[5])); - TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[6], bytes[7])); - TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[8], bytes[9])); - TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15])); - return TRY_OR_THROW_OOM(vm, builder.to_string()); -} - -void Crypto::visit_edges(Cell::Visitor& visitor) -{ - Base::visit_edges(visitor); - visitor.visit(m_subtle.ptr()); -} + TRY(builder.try_appendff("{:02x}{:02x}{:02x}{:02x}-", bytes[0], bytes[1], bytes[2], bytes[3])); + TRY(builder.try_appendff("{:02x}{:02x}-", bytes[4], bytes[5])); + TRY(builder.try_appendff("{:02x}{:02x}-", bytes[6], bytes[7])); + TRY(builder.try_appendff("{:02x}{:02x}-", bytes[8], bytes[9])); + TRY(builder.try_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 bbef73827e..c59b2b6907 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.h +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.h @@ -35,4 +35,6 @@ private: JS::GCPtr<SubtleCrypto> m_subtle; }; +ErrorOr<String> generate_random_uuid(); + } |