Last active
August 20, 2016 12:52
-
-
Save zno5/01dfa825611139bb1fedffb9d2305e40 to your computer and use it in GitHub Desktop.
Calculate a SHA-256 hash with OpenSSL 1.0.2h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdint> | |
#include <vector> | |
#include <array> | |
#include <openssl/evp.h> | |
#include <openssl/sha.h> | |
namespace { | |
bool Sha256( | |
std::array<uint8_t, SHA256_DIGEST_LENGTH> & out, | |
const std::vector<uint8_t> & in | |
) | |
{ | |
auto ctx = EVP_MD_CTX_create(); | |
if (!ctx) | |
{ | |
return false; | |
} | |
bool result = false; | |
for (;;) | |
{ | |
if (EVP_DigestInit(ctx, EVP_sha256()) != 1) | |
{ | |
break; | |
} | |
if (EVP_DigestUpdate(ctx, in.data(), in.size()) != 1) | |
{ | |
break; | |
} | |
if (EVP_DigestFinal(ctx, out.data(), nullptr) != 1) | |
{ | |
break; | |
} | |
result = true; | |
break; | |
} | |
EVP_MD_CTX_destroy(ctx); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment