Skip to content

Commit

Permalink
fixup! crypto: use EVP_MD_fetch and cache EVP_MD for hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Dec 3, 2023
1 parent b9bcd67 commit 25ee824
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,22 @@ void Hash::New(const FunctionCallbackInfo<Value>& 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<unsigned int> xof_md_len = Nothing<unsigned int>();
Expand Down

0 comments on commit 25ee824

Please sign in to comment.