The code below is the method by which a SHA152 hash is computed through using OpenSSL, however according to the changelog OpenSSL has supported SHA512/256 for some time - the issue is I cannot find information about how to use it?! If anyone knows how I'd be very grateful if you could let me know how.
SHA512_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, inFile)) != 0)
SHA512_Update (&mdContext, data, bytes);
SHA512_Final (c,&mdContext);
for(i = 0; i < SHA512_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf (" %s\n", filename);
return 0;
(this code is just an example, I know it will not run just like that)
EDIT: I've just changed SHA512_DIGEST_LENGTH
to SHA256_DIGEST_LENGTH
, is this the correct method? I'm concerned this will still compute the entire SHA512 hash (I'd like to see whether there is a performance increase) Not entirely sure how it works, so apologies in advance!)
Assume you are using a linux environment, you can reference the sha512-256 example (evptest.c) below:
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#define DIGEST_NAME "sha512-256"
int main(int argc, char *argv[]) {
EVP_MD_CTX *mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(DIGEST_NAME);
if(!md) {
printf("Unknown message digest %s\n", DIGEST_NAME);
exit(1);
}
mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
printf("Digest %s is: ", DIGEST_NAME);
for(i = 0; i < md_len; i++)
printf("%02x", md_value[i]);
printf("\n");
/* Call this once before exit. */
EVP_cleanup();
exit(0);
}
In Linux with latest OpenSSL and build-essential, you can compile it:
gcc evptest.c -l crypto -o evptest
Then execute the evptest binary, you will see sha512-256 result
./evptest
Digest sha512-256 is: dd889041ed0cf4ff7a1cf77dd384444b2de9347b032b95e64fc1955d889da8fd
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.