-
Notifications
You must be signed in to change notification settings - Fork 81
Migrate hash.cc from deprecated SHA256_* to EVP API #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include <openssl/sha.h> | ||
| #include <openssl/evp.h> | ||
|
|
||
| namespace proxy_wasm { | ||
|
|
||
|
|
@@ -37,13 +37,33 @@ std::string BytesToHex(const std::vector<uint8_t> &bytes) { | |
| } // namespace | ||
|
|
||
| std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts) { | ||
| uint8_t sha256[SHA256_DIGEST_LENGTH]; | ||
| SHA256_CTX sha_ctx; | ||
| SHA256_Init(&sha_ctx); | ||
| for (auto part : parts) { | ||
| SHA256_Update(&sha_ctx, part.data(), part.size()); | ||
| uint8_t sha256[32]; // SHA-256 produces 32 bytes | ||
| unsigned int hash_len = 0; | ||
|
|
||
| EVP_MD_CTX *hash_ctx = EVP_MD_CTX_new(); | ||
| if (hash_ctx == nullptr) { | ||
| return std::vector<uint8_t>(); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: consider using absl::Cleanup to guarantee the EVP_MD_CTX_free call at end of scope, and not have to maintain/check hash_ok, i.e. something like: |
||
|
|
||
| bool hash_ok = (EVP_DigestInit_ex(hash_ctx, EVP_sha256(), nullptr) != 0); | ||
| if (hash_ok) { | ||
| for (auto part : parts) { | ||
| if (EVP_DigestUpdate(hash_ctx, part.data(), part.size()) == 0) { | ||
| hash_ok = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (hash_ok) { | ||
| hash_ok = (EVP_DigestFinal_ex(hash_ctx, sha256, &hash_len) != 0); | ||
| } | ||
| SHA256_Final(sha256, &sha_ctx); | ||
|
|
||
| EVP_MD_CTX_free(hash_ctx); | ||
|
|
||
| if (!hash_ok || hash_len != 32) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than hardcoding 32, suggest |
||
| return std::vector<uint8_t>(); | ||
| } | ||
|
|
||
| return std::vector<uint8_t>(std::begin(sha256), std::end(sha256)); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using EVP_MAX_MD_SIZE here, just to avoid hardcoding a value and having to document the assumption.