/* * Copyright (c) 2021-2022, Linus Groh * Copyright (c) 2023, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include namespace Web::Crypto { class SubtleCrypto final : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject); JS_DECLARE_ALLOCATOR(SubtleCrypto); struct RegisteredAlgorithm { NonnullOwnPtr (*create_methods)(JS::Realm&) = nullptr; JS::ThrowCompletionOr> (*parameter_from_value)(JS::VM&, JS::Value) = nullptr; }; using SupportedAlgorithmsMap = HashMap>; public: [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&); virtual ~SubtleCrypto() override; JS::NonnullGCPtr digest(AlgorithmIdentifier const& algorithm, JS::Handle const& data); JS::ThrowCompletionOr> generate_key(AlgorithmIdentifier algorithm, bool extractable, Vector key_usages); JS::ThrowCompletionOr> import_key(Bindings::KeyFormat format, KeyDataType key_data, AlgorithmIdentifier algorithm, bool extractable, Vector key_usages); JS::ThrowCompletionOr> export_key(Bindings::KeyFormat format, JS::NonnullGCPtr key); private: explicit SubtleCrypto(JS::Realm&); virtual void initialize(JS::Realm&) override; struct NormalizedAlgorithmAndParameter { NonnullOwnPtr methods; NonnullOwnPtr parameter; }; WebIDL::ExceptionOr normalize_an_algorithm(AlgorithmIdentifier const& algorithm, String operation); static SubtleCrypto::SupportedAlgorithmsMap& supported_algorithms_internal(); static SubtleCrypto::SupportedAlgorithmsMap supported_algorithms(); template static void define_an_algorithm(String op, String algorithm); }; }