1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibWeb: Port SubtleCrypto to new String

The algorithm comparison in digest() is done using a StringView to avoid
complex error handling and promise rejection.
This commit is contained in:
Kenneth Myhra 2023-02-23 21:31:57 +01:00 committed by Linus Groh
parent a8cef1fa06
commit f783af05ed
3 changed files with 8 additions and 7 deletions

View file

@ -35,7 +35,7 @@ JS::ThrowCompletionOr<void> SubtleCrypto::initialize(JS::Realm& realm)
} }
// https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest // https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest
JS::Promise* SubtleCrypto::digest(DeprecatedString const& algorithm, JS::Handle<JS::Object> const& data) JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data)
{ {
auto& realm = this->realm(); 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". // 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. // FIXME: This is way more generic than it needs to be right now, so we simplify it.
::Crypto::Hash::HashKind hash_kind; ::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; 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; 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; 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; hash_kind = ::Crypto::Hash::HashKind::SHA512;
} }
// 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm.

View file

@ -19,7 +19,7 @@ public:
virtual ~SubtleCrypto() override; virtual ~SubtleCrypto() override;
JS::Promise* digest(DeprecatedString const& algorithm, JS::Handle<JS::Object> const& data); JS::Promise* digest(String const& algorithm, JS::Handle<JS::Object> const& data);
private: private:
explicit SubtleCrypto(JS::Realm&); explicit SubtleCrypto(JS::Realm&);

View file

@ -1,4 +1,4 @@
[SecureContext,Exposed=(Window,Worker)] [SecureContext,Exposed=(Window,Worker), UseNewAKString]
interface SubtleCrypto { interface SubtleCrypto {
// FIXME: Add support for AlgorithmIdentifier ("typedef (object or DOMString)") // FIXME: Add support for AlgorithmIdentifier ("typedef (object or DOMString)")
Promise<any> digest(DOMString algorithm, BufferSource data); Promise<any> digest(DOMString algorithm, BufferSource data);