Skip to content

Instantly share code, notes, and snippets.

@zno5
Last active August 20, 2016 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zno5/01dfa825611139bb1fedffb9d2305e40 to your computer and use it in GitHub Desktop.
Save zno5/01dfa825611139bb1fedffb9d2305e40 to your computer and use it in GitHub Desktop.
Calculate a SHA-256 hash with OpenSSL 1.0.2h
#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