From f783af05ed83b358cca9cbfec1a6a9f10dbebdc9 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 23 Feb 2023 21:31:57 +0100 Subject: [PATCH] LibWeb: Port SubtleCrypto to new String The algorithm comparison in digest() is done using a StringView to avoid complex error handling and promise rejection. --- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp | 11 ++++++----- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h | 2 +- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index fb64d3a1df..5bb9104f1c 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -35,7 +35,7 @@ JS::ThrowCompletionOr SubtleCrypto::initialize(JS::Realm& realm) } // https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest -JS::Promise* SubtleCrypto::digest(DeprecatedString const& algorithm, JS::Handle const& data) +JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle const& data) { auto& realm = this->realm(); @@ -54,13 +54,14 @@ JS::Promise* SubtleCrypto::digest(DeprecatedString const& algorithm, JS::Handle< // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest". // FIXME: This is way more generic than it needs to be right now, so we simplify it. ::Crypto::Hash::HashKind hash_kind; - if (algorithm.equals_ignoring_case("SHA-1"sv)) { + auto algorithm_as_string_view = algorithm.bytes_as_string_view(); + if (algorithm_as_string_view.equals_ignoring_case("SHA-1"sv)) { hash_kind = ::Crypto::Hash::HashKind::SHA1; - } else if (algorithm.equals_ignoring_case("SHA-256"sv)) { + } else if (algorithm_as_string_view.equals_ignoring_case("SHA-256"sv)) { hash_kind = ::Crypto::Hash::HashKind::SHA256; - } else if (algorithm.equals_ignoring_case("SHA-384"sv)) { + } else if (algorithm_as_string_view.equals_ignoring_case("SHA-384"sv)) { hash_kind = ::Crypto::Hash::HashKind::SHA384; - } else if (algorithm.equals_ignoring_case("SHA-512"sv)) { + } else if (algorithm_as_string_view.equals_ignoring_case("SHA-512"sv)) { hash_kind = ::Crypto::Hash::HashKind::SHA512; } // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index 2139c4b690..0113cf16b1 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -19,7 +19,7 @@ public: virtual ~SubtleCrypto() override; - JS::Promise* digest(DeprecatedString const& algorithm, JS::Handle const& data); + JS::Promise* digest(String const& algorithm, JS::Handle const& data); private: explicit SubtleCrypto(JS::Realm&); diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl index 99f867b2c6..588d37074c 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl @@ -1,4 +1,4 @@ -[SecureContext,Exposed=(Window,Worker)] +[SecureContext,Exposed=(Window,Worker), UseNewAKString] interface SubtleCrypto { // FIXME: Add support for AlgorithmIdentifier ("typedef (object or DOMString)") Promise digest(DOMString algorithm, BufferSource data);