mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
LibWeb: Centralize getting the hash algorithm identifier for crypto
This commit is contained in:
parent
4e1dab477a
commit
f73a434177
Notes:
github-actions[bot]
2024-11-14 10:53:26 +00:00
Author: https://github.com/gmta Commit: https://github.com/LadybirdBrowser/ladybird/commit/f73a434177a Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2331
1 changed files with 19 additions and 29 deletions
|
@ -34,6 +34,17 @@
|
||||||
|
|
||||||
namespace Web::Crypto {
|
namespace Web::Crypto {
|
||||||
|
|
||||||
|
static JS::ThrowCompletionOr<HashAlgorithmIdentifier> hash_algorithm_identifier_from_value(JS::VM& vm, JS::Value hash_value)
|
||||||
|
{
|
||||||
|
if (hash_value.is_string()) {
|
||||||
|
auto hash_string = TRY(hash_value.to_string(vm));
|
||||||
|
return HashAlgorithmIdentifier { hash_string };
|
||||||
|
}
|
||||||
|
|
||||||
|
auto hash_object = TRY(hash_value.to_object(vm));
|
||||||
|
return HashAlgorithmIdentifier { hash_object };
|
||||||
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/webcrypto/#concept-usage-intersection
|
// https://w3c.github.io/webcrypto/#concept-usage-intersection
|
||||||
static Vector<Bindings::KeyUsage> usage_intersection(ReadonlySpan<Bindings::KeyUsage> a, ReadonlySpan<Bindings::KeyUsage> b)
|
static Vector<Bindings::KeyUsage> usage_intersection(ReadonlySpan<Bindings::KeyUsage> a, ReadonlySpan<Bindings::KeyUsage> b)
|
||||||
{
|
{
|
||||||
|
@ -337,7 +348,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> HKDFParams::from_value(JS:
|
||||||
auto name = TRY(name_value.to_string(vm));
|
auto name = TRY(name_value.to_string(vm));
|
||||||
|
|
||||||
auto hash_value = TRY(object.get("hash"));
|
auto hash_value = TRY(object.get("hash"));
|
||||||
auto hash = TRY(hash_value.to_string(vm));
|
auto hash = TRY(hash_algorithm_identifier_from_value(vm, hash_value));
|
||||||
|
|
||||||
auto salt_value = TRY(object.get("salt"));
|
auto salt_value = TRY(object.get("salt"));
|
||||||
if (!salt_value.is_object() || !(is<JS::TypedArrayBase>(salt_value.as_object()) || is<JS::ArrayBuffer>(salt_value.as_object()) || is<JS::DataView>(salt_value.as_object())))
|
if (!salt_value.is_object() || !(is<JS::TypedArrayBase>(salt_value.as_object()) || is<JS::ArrayBuffer>(salt_value.as_object()) || is<JS::DataView>(salt_value.as_object())))
|
||||||
|
@ -372,7 +383,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> PBKDF2Params::from_value(J
|
||||||
auto iterations = TRY(iterations_value.to_u32(vm));
|
auto iterations = TRY(iterations_value.to_u32(vm));
|
||||||
|
|
||||||
auto hash_value = TRY(object.get("hash"));
|
auto hash_value = TRY(object.get("hash"));
|
||||||
auto hash = TRY(hash_value.to_string(vm));
|
auto hash = TRY(hash_algorithm_identifier_from_value(vm, hash_value));
|
||||||
|
|
||||||
return adopt_own<AlgorithmParams>(*new PBKDF2Params { name, salt, iterations, hash });
|
return adopt_own<AlgorithmParams>(*new PBKDF2Params { name, salt, iterations, hash });
|
||||||
}
|
}
|
||||||
|
@ -421,16 +432,9 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedKeyGenParams::fro
|
||||||
public_exponent = static_cast<JS::Uint8Array&>(public_exponent_value.as_object());
|
public_exponent = static_cast<JS::Uint8Array&>(public_exponent_value.as_object());
|
||||||
|
|
||||||
auto hash_value = TRY(object.get("hash"));
|
auto hash_value = TRY(object.get("hash"));
|
||||||
auto hash = Variant<Empty, HashAlgorithmIdentifier> { Empty {} };
|
auto hash = TRY(hash_algorithm_identifier_from_value(vm, hash_value));
|
||||||
if (hash_value.is_string()) {
|
|
||||||
auto hash_string = TRY(hash_value.to_string(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_string };
|
|
||||||
} else {
|
|
||||||
auto hash_object = TRY(hash_value.to_object(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_object };
|
|
||||||
}
|
|
||||||
|
|
||||||
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent), hash.get<HashAlgorithmIdentifier>() });
|
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent), hash });
|
||||||
}
|
}
|
||||||
|
|
||||||
RsaHashedImportParams::~RsaHashedImportParams() = default;
|
RsaHashedImportParams::~RsaHashedImportParams() = default;
|
||||||
|
@ -443,16 +447,9 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedImportParams::fro
|
||||||
auto name = TRY(name_value.to_string(vm));
|
auto name = TRY(name_value.to_string(vm));
|
||||||
|
|
||||||
auto hash_value = TRY(object.get("hash"));
|
auto hash_value = TRY(object.get("hash"));
|
||||||
auto hash = Variant<Empty, HashAlgorithmIdentifier> { Empty {} };
|
auto hash = TRY(hash_algorithm_identifier_from_value(vm, hash_value));
|
||||||
if (hash_value.is_string()) {
|
|
||||||
auto hash_string = TRY(hash_value.to_string(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_string };
|
|
||||||
} else {
|
|
||||||
auto hash_object = TRY(hash_value.to_object(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_object };
|
|
||||||
}
|
|
||||||
|
|
||||||
return adopt_own<AlgorithmParams>(*new RsaHashedImportParams { name, hash.get<HashAlgorithmIdentifier>() });
|
return adopt_own<AlgorithmParams>(*new RsaHashedImportParams { name, hash });
|
||||||
}
|
}
|
||||||
|
|
||||||
RsaOaepParams::~RsaOaepParams() = default;
|
RsaOaepParams::~RsaOaepParams() = default;
|
||||||
|
@ -487,16 +484,9 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> EcdsaParams::from_value(JS
|
||||||
auto name = TRY(name_value.to_string(vm));
|
auto name = TRY(name_value.to_string(vm));
|
||||||
|
|
||||||
auto hash_value = TRY(object.get("hash"));
|
auto hash_value = TRY(object.get("hash"));
|
||||||
auto hash = Variant<Empty, HashAlgorithmIdentifier> { Empty {} };
|
auto hash = TRY(hash_algorithm_identifier_from_value(vm, hash_value));
|
||||||
if (hash_value.is_string()) {
|
|
||||||
auto hash_string = TRY(hash_value.to_string(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_string };
|
|
||||||
} else {
|
|
||||||
auto hash_object = TRY(hash_value.to_object(vm));
|
|
||||||
hash = HashAlgorithmIdentifier { hash_object };
|
|
||||||
}
|
|
||||||
|
|
||||||
return adopt_own<AlgorithmParams>(*new EcdsaParams { name, hash.get<HashAlgorithmIdentifier>() });
|
return adopt_own<AlgorithmParams>(*new EcdsaParams { name, hash });
|
||||||
}
|
}
|
||||||
|
|
||||||
EcKeyGenParams::~EcKeyGenParams() = default;
|
EcKeyGenParams::~EcKeyGenParams() = default;
|
||||||
|
|
Loading…
Reference in a new issue