Created
January 27, 2022 23:19
-
-
Save idesai/9a18ec8a9fb38dd5c022de86259081b3 to your computer and use it in GitHub Desktop.
openSSL sha256 calculation
This file contains hidden or 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 <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <openssl/buffer.h> | |
#include <openssl/evp.h> | |
#include <openssl/sha.h> | |
int main(void) { | |
unsigned char *hash = (unsigned char*) malloc(SHA256_DIGEST_LENGTH); | |
if (!hash) { | |
printf("OOM"); | |
return 1; | |
} | |
EVP_MD_CTX *sha256 = EVP_MD_CTX_new(); | |
int is_success = EVP_DigestInit(sha256, EVP_sha256()); | |
if (!is_success) { | |
printf("EVP_DigestInit failed"); | |
return 1; | |
} | |
uint8_t test[1] = {0}; | |
is_success = EVP_DigestUpdate(sha256, &test, 0); | |
if (!is_success) { | |
printf("EVP_DigestUpdate failed"); | |
return 1; | |
} | |
is_success = EVP_DigestFinal_ex(sha256, hash, NULL); | |
if (!is_success) { | |
printf("EVP_DigestFinal failed"); | |
return 1; | |
} | |
EVP_MD_CTX_free(sha256); | |
unsigned i; | |
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { | |
printf("%02X", hash[i]); | |
} | |
printf("\n"); | |
return 0; | |
} | |
/* | |
* Compile: gcc -g -o calc_sha256 calc_sha256.c -lssl -lcrypto | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment