This tutorial will guide you on how to hash a string by using OpenSSL’s SHA384 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.
- Here are the openssl SHA384 sample source code.
Example #1: sha384_sample1.cpp1234567891011121314151617181920#include <stdio.h>#include <string.h>#include <openssl/sha.h>int main(){unsigned char digest[SHA384_DIGEST_LENGTH];char string[] = "hello world";SHA384((unsigned char*)&string, strlen(string), (unsigned char*)&digest);char mdString[SHA384_DIGEST_LENGTH*2+1];for(int i = 0; i < SHA384_DIGEST_LENGTH; i++)sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);printf("SHA384 digest: %s\n", mdString);return 0;}
Example #2: sha384_sample2.cpp12345678910111213141516171819202122#include <stdio.h>#include <string.h>#include <openssl/sha.h>int main() {unsigned char digest[SHA512_DIGEST_LENGTH];const char* string = "hello world";SHA512_CTX ctx;SHA384_Init(&ctx);SHA384_Update(&ctx, string, strlen(string));SHA384_Final(digest, &ctx);char mdString[SHA384_DIGEST_LENGTH*2+1];for (int i = 0; i < SHA384_DIGEST_LENGTH; i++)sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);printf("SHA384 digest: %s\n", mdString);return 0;} - Let’s try to compile both sample cpp files and you should observe the following output screenshot.
Note: -lcrypto will include the crypto library from openssl~$ gcc sha384_sample1.cpp -o sample1 -lcrypto
~$ ./sample1
SHA384 digest: fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
~$ gcc sha384_sample2.cpp -o sample2 -lcrypto
~$ ./sample2
SHA384 digest: fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
hosenhoseni