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;