Skip to content

Commit

Permalink
updated openssl MD5 sum calculation API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
fangfufu committed Aug 26, 2024
1 parent 3e45c0e commit dd58263
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "config.h"
#include "log.h"

#include <openssl/md5.h>
#include <openssl/evp.h>
#include <uuid/uuid.h>

#include <errno.h>
Expand Down Expand Up @@ -114,17 +114,27 @@ char *generate_salt(void)

char *generate_md5sum(const char *str)
{
MD5_CTX c;
unsigned char md5[MD5_DIGEST_LENGTH];
size_t len = strnlen(str, MAX_PATH_LEN);
char *out = CALLOC(MD5_HASH_LEN + 1, sizeof(char));

MD5_Init(&c);
MD5_Update(&c, str, len);
MD5_Final(md5, &c);
EVP_MD_CTX *mdctx;
unsigned char *md5_digest;
unsigned int md5_digest_len = EVP_MD_size(EVP_md5());

for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(out + 2 * i, "%02x", md5[i]);
// MD5_Init
mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);

// MD5_Update
EVP_DigestUpdate(mdctx, str, len);

// MD5_Final
md5_digest = (unsigned char *)OPENSSL_malloc(md5_digest_len);
EVP_DigestFinal_ex(mdctx, md5_digest, &md5_digest_len);
EVP_MD_CTX_free(mdctx);

for (unsigned int i = 0; i < md5_digest_len; i++) {
snprintf(out + 2 * i, MD5_HASH_LEN + 1, "%02x", md5_digest[i]);
}
return out;
}
Expand Down

0 comments on commit dd58263

Please sign in to comment.