Skip to content

Commit 14d90d8

Browse files
committed
md5: use OpenSSL 1.1.0+ API
1 parent 68cb3cb commit 14d90d8

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/md5.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "cpdup.h"
1010

11-
#include <openssl/md5.h>
11+
#include <openssl/evp.h>
1212

1313
typedef struct MD5Node {
1414
struct MD5Node *md_Next;
@@ -257,13 +257,14 @@ md5_check(const char *spath, const char *dpath)
257257
static char *
258258
md5_file(const char *filename, char *buf)
259259
{
260-
unsigned char digest[MD5_DIGEST_LENGTH];
260+
unsigned char digest[EVP_MAX_MD_SIZE];
261261
static const char hex[] = "0123456789abcdef";
262-
MD5_CTX ctx;
262+
EVP_MD_CTX *ctx;
263263
unsigned char buffer[4096];
264264
struct stat st;
265265
off_t size;
266-
int fd, bytes, i;
266+
int fd, bytes;
267+
unsigned int i, md_len;
267268

268269
fd = open(filename, O_RDONLY);
269270
if (fd < 0)
@@ -273,7 +274,11 @@ md5_file(const char *filename, char *buf)
273274
goto err;
274275
}
275276

276-
MD5_Init(&ctx);
277+
ctx = EVP_MD_CTX_new();
278+
if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL)) {
279+
fprintf(stderr, "Unable to initialize MD5 digest.\n");
280+
exit(1);
281+
}
277282
size = st.st_size;
278283
bytes = 0;
279284
while (size > 0) {
@@ -283,7 +288,10 @@ md5_file(const char *filename, char *buf)
283288
bytes = read(fd, buffer, size);
284289
if (bytes < 0)
285290
break;
286-
MD5_Update(&ctx, buffer, bytes);
291+
if (!EVP_DigestUpdate(ctx, buffer, bytes)) {
292+
fprintf(stderr, "Unable to update MD5 digest.\n");
293+
exit(1);
294+
}
287295
size -= bytes;
288296
}
289297

@@ -292,17 +300,23 @@ md5_file(const char *filename, char *buf)
292300
if (bytes < 0)
293301
return NULL;
294302

303+
if (!EVP_DigestFinal(ctx, digest, &md_len)) {
304+
fprintf(stderr, "Unable to finalize MD5 digest.\n");
305+
exit(1);
306+
}
307+
308+
EVP_MD_CTX_free(ctx);
309+
295310
if (!buf)
296-
buf = malloc(MD5_DIGEST_LENGTH * 2 + 1);
311+
buf = malloc(md_len * 2 + 1);
297312
if (!buf)
298313
return NULL;
299314

300-
MD5_Final(digest, &ctx);
301-
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
315+
for (i = 0; i < md_len; i++) {
302316
buf[2*i] = hex[digest[i] >> 4];
303317
buf[2*i+1] = hex[digest[i] & 0x0f];
304318
}
305-
buf[MD5_DIGEST_LENGTH * 2] = '\0';
319+
buf[md_len * 2] = '\0';
306320

307321
return buf;
308322
}

0 commit comments

Comments
 (0)