Skip to content

Commit 79ada6b

Browse files
committed
md5: use OpenSSL 1.1.0+ API
1 parent 68cb3cb commit 79ada6b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/md5.c

Lines changed: 22 additions & 10 deletions
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,21 @@ 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+
295308
if (!buf)
296-
buf = malloc(MD5_DIGEST_LENGTH * 2 + 1);
309+
buf = malloc(md_len * 2 + 1);
297310
if (!buf)
298311
return NULL;
299312

300-
MD5_Final(digest, &ctx);
301-
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
313+
for (i = 0; i < md_len; i++) {
302314
buf[2*i] = hex[digest[i] >> 4];
303315
buf[2*i+1] = hex[digest[i] & 0x0f];
304316
}
305-
buf[MD5_DIGEST_LENGTH * 2] = '\0';
317+
buf[md_len * 2] = '\0';
306318

307319
return buf;
308320
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy