Introduce calculate_digest convenience function

This function just calculates the chosen digest and returns the result
in either the passed buffer, or in a static buffer. Basically a superset
to the surprisingly popular internal_md5.
This commit is contained in:
Johanna Amann 2020-04-24 17:00:10 -07:00
parent 5e7915ae7a
commit d34532f847
2 changed files with 20 additions and 2 deletions

View file

@ -63,12 +63,18 @@ void hash_final(EVP_MD_CTX* c, u_char* md)
unsigned char* internal_md5(const unsigned char* data, unsigned long len, unsigned char* out)
{
static unsigned char static_out[MD5_DIGEST_LENGTH];
return calculate_digest(Hash_MD5, data, len, out);
}
unsigned char* calculate_digest(HashAlgorithm Alg, const unsigned char* data, uint64_t len, unsigned char* out)
{
// maximum possible length for supported hashes
static unsigned char static_out[SHA512_DIGEST_LENGTH];
if ( ! out )
out = static_out; // use static array for return, see OpenSSL man page
EVP_MD_CTX* c = hash_init(Hash_MD5);
EVP_MD_CTX* c = hash_init(Alg);
hash_update(c, data, len);
hash_final(c, out);
return out;

View file

@ -22,6 +22,8 @@ inline void* EVP_MD_CTX_md_data(const EVP_MD_CTX* ctx)
}
#endif
// if you add something here, note that you might have to make sure that the
// static_out member in calculate_digest is still long enough.
enum HashAlgorithm { Hash_MD5, Hash_SHA1, Hash_SHA224, Hash_SHA256, Hash_SHA384, Hash_SHA512 };
inline const char* digest_print(const u_char* digest, size_t n)
@ -54,3 +56,13 @@ void hash_update(EVP_MD_CTX* c, const void* data, unsigned long len);
void hash_final(EVP_MD_CTX* c, u_char* md);
unsigned char* internal_md5(const unsigned char* data, unsigned long len, unsigned char* out);
/**
* Calculates the selected digest.
* @param Alg Digest algorithm to use.
* @param data Data to hash.
* @param len Length of data to hash.
* @param out Buffer to write data to. If set to nullptr, a static buffer will be used
* @return Buffer that the hash was written to. Length is deoendent on the chosen hash function.
*/
unsigned char* calculate_digest(HashAlgorithm Alg, const unsigned char* data, uint64_t len, unsigned char* out);