Skip to content

Commit ec2c2e8

Browse files
federico-sysdigpoiana
authored andcommitted
chore: avoid deprecated funcs to calculate sha256
Signed-off-by: Federico Aponte <[email protected]>
1 parent 3277d6e commit ec2c2e8

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

userspace/engine/falco_utils.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ limitations under the License.
2323

2424
#include <re2/re2.h>
2525
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
26-
#include <openssl/sha.h>
26+
#include <openssl/evp.h>
2727
#endif
2828
#include <cstring>
2929
#include <fstream>
@@ -144,22 +144,22 @@ std::string calculate_file_sha256sum(const std::string& filename) {
144144
return "";
145145
}
146146

147-
SHA256_CTX sha256_context;
148-
SHA256_Init(&sha256_context);
147+
std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> ctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
148+
EVP_DigestInit_ex(ctx.get(), EVP_sha256(), nullptr);
149149

150150
constexpr size_t buffer_size = 4096;
151151
char buffer[buffer_size];
152152
while(file.read(buffer, buffer_size)) {
153-
SHA256_Update(&sha256_context, buffer, buffer_size);
153+
EVP_DigestUpdate(ctx.get(), buffer, buffer_size);
154154
}
155-
SHA256_Update(&sha256_context, buffer, file.gcount());
155+
EVP_DigestUpdate(ctx.get(), buffer, file.gcount());
156156

157-
unsigned char digest[SHA256_DIGEST_LENGTH];
158-
SHA256_Final(digest, &sha256_context);
157+
std::vector<uint8_t> digest(EVP_MD_size(EVP_sha256()));
158+
EVP_DigestFinal_ex(ctx.get(), digest.data(), nullptr);
159159

160-
std::stringstream ss;
161-
for(int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
162-
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned>(digest[i]);
160+
std::ostringstream ss;
161+
for(auto& c : digest) {
162+
ss << std::hex << std::setw(2) << std::setfill('0') << (int)c;
163163
}
164164
return ss.str();
165165
}

0 commit comments

Comments
 (0)