diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index cc61dad3d2a762..f3a86fcde84765 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -98,19 +98,22 @@ void Hash::New(const FunctionCallbackInfo& args) { std::string hash_type_str = hash_type.ToString(); auto it = env->evp_md_cache.find(hash_type_str); if (it == env->evp_md_cache.end()) { - EVP_MD* maybe_md = EVP_MD_fetch(nullptr, hash_type_str.c_str(), nullptr); - if (maybe_md == nullptr) { - return ThrowCryptoError( - env, ERR_get_error(), "Digest method not supported"); + EVP_MD* explicit_md = EVP_MD_fetch(nullptr, hash_type_str.c_str(), nullptr); + if (explicit_md != nullptr) { + md = explicit_md; + env->evp_md_cache.emplace(hash_type_str, explicit_md); + } else { + // We'll do a fallback. + ERR_clear_error(); } - md = maybe_md; - env->evp_md_cache.emplace(hash_type_str, maybe_md); } else { md = it->second.get(); } -#else - md = EVP_get_digestbyname(*hash_type); #endif // OPENSSL_VERSION_MAJOR >= 3 + // EVP_MD_fetch failed, fallback to EVP_get_digestbyname. + if (md == nullptr) { + md = EVP_get_digestbyname(*hash_type); + } } Maybe xof_md_len = Nothing();