Created
February 5, 2019 02:31
-
-
Save devendranaga/5a79e166210f40af25f66c97a7121608 to your computer and use it in GitHub Desktop.
create hash with openssl
This file contains 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
/** | |
* Author: Devendra Naga <devendra.aaru@gmail.com> | |
* | |
* LICENSE MIT | |
*/ | |
#include <iostream> | |
#include <string> | |
// for ERR_ functions | |
#include <openssl/err.h> | |
// for openssl_config | |
#include <openssl/conf.h> | |
// hash and EVP based API | |
#include <openssl/md5.h> | |
#include <openssl/evp.h> | |
// for RAND_ | |
#include <openssl/rand.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
class hash_class { | |
public: | |
hash_class() { // initialise the openSSL crypto lib | |
ERR_load_crypto_strings(); | |
OpenSSL_add_all_algorithms(); | |
OPENSSL_config(NULL); | |
RAND_poll(); | |
} | |
~hash_class() { // deinitialise the openSSL crypto lib | |
EVP_cleanup(); | |
CRYPTO_cleanup_all_ex_data(); | |
ERR_free_strings(); | |
} | |
int generate_md5(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_md5(), digest); | |
} | |
int generate_sha(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha(), digest); | |
} | |
int generate_sha1(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha1(), digest); | |
} | |
int generate_sha224(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha224(), digest); | |
} | |
int generate_sha256(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha256(), digest); | |
} | |
int generate_sha384(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha384(), digest); | |
} | |
int generate_sha512(uint8_t *msg, size_t msglen, uint8_t *digest) { | |
return gen_hash(msg, msglen, EVP_sha512(), digest); | |
} | |
int dump_hash(uint8_t *hash, int hash_len) { | |
int i; | |
printf("hash: "); | |
for (i = 0; i < hash_len; i ++) { | |
printf("%02x", hash[i] & 0xff); | |
} | |
printf("\n"); | |
} | |
private: | |
int gen_hash(uint8_t *msg, size_t msg_len, const EVP_MD *md, uint8_t *digest) | |
{ | |
int ret; | |
EVP_MD_CTX *ctx; | |
// create md context | |
ctx = EVP_MD_CTX_create(); | |
if (!ctx) { | |
return -1; | |
} | |
// initialise digest | |
ret = EVP_DigestInit(ctx, md); | |
if (ret != 1) { | |
goto bad; | |
} | |
// we have one buf.. just call once | |
ret = EVP_DigestUpdate(ctx, msg, msg_len); | |
if (ret != 1) { | |
goto bad; | |
} | |
uint32_t digest_len; | |
// call final digest to get the digest value | |
ret = EVP_DigestFinal_ex(ctx, digest, &digest_len); | |
if (ret != 1) { | |
goto bad; | |
} | |
// free up md_ctx | |
EVP_MD_CTX_destroy(ctx); | |
return digest_len; | |
bad: | |
EVP_MD_CTX_destroy(ctx); | |
return -1; | |
} | |
}; | |
// test hash class | |
int main() | |
{ | |
hash_class h_; | |
uint8_t hex[32]; | |
int hash_len; | |
std::string msg = "testing hash class"; | |
hash_len = h_.generate_sha256((uint8_t *)msg.c_str(), msg.length(), hex); | |
h_.dump_hash(hex, hash_len); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment