Last active
June 6, 2022 02:57
-
-
Save dvtalk/e08f32aa862b24985da1c7806b6b890b to your computer and use it in GitHub Desktop.
Sha hash function operation using OpenSSL library
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
| // https://dvtalk.me | |
| #ifndef __SHA_HASH_C__ | |
| #define __SHA_HASH_C__ | |
| #include <openssl/conf.h> | |
| #include <openssl/evp.h> | |
| #include <openssl/err.h> | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #ifndef __MAIN__ | |
| #define __MAIN__ | |
| int main (void) { | |
| unsigned char *hashmessage = (unsigned char *)"If there was an abundance of misery in the world, there was also sufficient joy, yes - as long as one knew where to look for it."; | |
| unsigned int hashcode_len; | |
| unsigned char hashcode[4096]; | |
| hashcode_len = sha_digest(SHA_384, | |
| hashmessage, | |
| strlen((char *)hashmessage), | |
| hashcode); | |
| /* Clean up */ | |
| EVP_cleanup(); | |
| ERR_free_strings(); | |
| return 0; | |
| } | |
| #endif | |
| #ifndef __HANDLE_ERRORS__ | |
| #define __HANDLE_ERRORS__ | |
| void handleErrors(void) { | |
| ERR_print_errors_fp(stderr); | |
| abort(); | |
| } | |
| #endif | |
| unsigned int sha_digest(enum SHA_OP_MODE sha_mode, | |
| unsigned char *plaintext, | |
| int plaintext_len, | |
| unsigned char *hashcode) { | |
| /* Initialise the library */ | |
| ERR_load_crypto_strings(); | |
| OpenSSL_add_all_digests(); | |
| // OPENSSL_config(NULL); | |
| const EVP_MD *hashptr; | |
| unsigned int hashcode_len; | |
| switch (sha_mode) { | |
| case SHA_1: if ((hashptr = EVP_sha1()) == NULL) handleErrors(); break; | |
| case SHA_224: if ((hashptr = EVP_sha224()) == NULL) handleErrors(); break; | |
| case SHA_256: if ((hashptr = EVP_sha256()) == NULL) handleErrors(); break; | |
| case SHA_384: if ((hashptr = EVP_sha384()) == NULL) handleErrors(); break; | |
| case SHA_512: if ((hashptr = EVP_sha512()) == NULL) handleErrors(); break; | |
| case SHA_512_224: if ((hashptr = EVP_sha512_224()) == NULL) handleErrors(); break; | |
| case SHA_512_256: if ((hashptr = EVP_sha512_256()) == NULL) handleErrors(); break; | |
| default: if ((hashptr = EVP_sha1()) == NULL) handleErrors(); break; | |
| } | |
| EVP_MD_CTX *hashctx = EVP_MD_CTX_create(); | |
| EVP_DigestInit_ex(hashctx, hashptr, NULL); | |
| EVP_DigestUpdate(hashctx, plaintext, plaintext_len); | |
| EVP_DigestFinal_ex(hashctx, hashcode, &hashcode_len); | |
| EVP_MD_CTX_destroy(hashctx); | |
| printf("\nPlain text length:%d bytes \nPlaintext is:\n", plaintext_len); | |
| BIO_dump_fp (stdout, (const char *)plaintext, plaintext_len); | |
| printf("\nHash code length:%d bytes \nHashcode is:\n", hashcode_len); | |
| BIO_dump_fp (stdout, (const char *)hashcode, hashcode_len); | |
| return hashcode_len; | |
| } | |
| #endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment