Skip to content

Commit 943643f

Browse files
mmatuskaliweitianux
authored andcommitted
md5: use OpenSSL 1.1.0+ API
This commit is having several issues, which I'll fix them and improve in later commits. From: PR#3 (#3)
1 parent ad36925 commit 943643f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/md5.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include "cpdup.h"
3535

36-
#include <openssl/md5.h>
36+
#include <openssl/evp.h>
3737

3838
typedef struct MD5Node {
3939
struct MD5Node *md_Next;
@@ -282,13 +282,14 @@ md5_check(const char *spath, const char *dpath)
282282
static char *
283283
md5_file(const char *filename, char *buf)
284284
{
285-
unsigned char digest[MD5_DIGEST_LENGTH];
285+
unsigned char digest[EVP_MAX_MD_SIZE];
286286
static const char hex[] = "0123456789abcdef";
287-
MD5_CTX ctx;
287+
EVP_MD_CTX *ctx;
288288
unsigned char buffer[4096];
289289
struct stat st;
290290
off_t size;
291-
int fd, bytes, i;
291+
int fd, bytes;
292+
unsigned int i, md_len;
292293

293294
fd = open(filename, O_RDONLY);
294295
if (fd < 0)
@@ -298,7 +299,11 @@ md5_file(const char *filename, char *buf)
298299
goto err;
299300
}
300301

301-
MD5_Init(&ctx);
302+
ctx = EVP_MD_CTX_new();
303+
if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL)) {
304+
fprintf(stderr, "Unable to initialize MD5 digest.\n");
305+
exit(1);
306+
}
302307
size = st.st_size;
303308
bytes = 0;
304309
while (size > 0) {
@@ -308,7 +313,11 @@ md5_file(const char *filename, char *buf)
308313
bytes = read(fd, buffer, size);
309314
if (bytes < 0)
310315
break;
311-
MD5_Update(&ctx, buffer, bytes);
316+
if (!EVP_DigestUpdate(ctx, buffer, bytes)) {
317+
EVP_MD_CTX_free(ctx);
318+
fprintf(stderr, "Unable to update MD5 digest.\n");
319+
exit(1);
320+
}
312321
size -= bytes;
313322
}
314323

@@ -317,17 +326,23 @@ md5_file(const char *filename, char *buf)
317326
if (bytes < 0)
318327
return NULL;
319328

329+
if (!EVP_DigestFinal(ctx, digest, &md_len)) {
330+
EVP_MD_CTX_free(ctx);
331+
fprintf(stderr, "Unable to finalize MD5 digest.\n");
332+
exit(1);
333+
}
334+
EVP_MD_CTX_free(ctx);
335+
320336
if (!buf)
321-
buf = malloc(MD5_DIGEST_LENGTH * 2 + 1);
337+
buf = malloc(md_len * 2 + 1);
322338
if (!buf)
323339
return NULL;
324340

325-
MD5_Final(digest, &ctx);
326-
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
341+
for (i = 0; i < md_len; i++) {
327342
buf[2*i] = hex[digest[i] >> 4];
328343
buf[2*i+1] = hex[digest[i] & 0x0f];
329344
}
330-
buf[MD5_DIGEST_LENGTH * 2] = '\0';
345+
buf[md_len * 2] = '\0';
331346

332347
return buf;
333348
}

0 commit comments

Comments
 (0)