8
8
9
9
#include "cpdup.h"
10
10
11
- #include <openssl/md5 .h>
11
+ #include <openssl/evp .h>
12
12
13
13
typedef struct MD5Node {
14
14
struct MD5Node * md_Next ;
@@ -257,13 +257,14 @@ md5_check(const char *spath, const char *dpath)
257
257
static char *
258
258
md5_file (const char * filename , char * buf )
259
259
{
260
- unsigned char digest [MD5_DIGEST_LENGTH ];
260
+ unsigned char digest [EVP_MAX_MD_SIZE ];
261
261
static const char hex [] = "0123456789abcdef" ;
262
- MD5_CTX ctx ;
262
+ EVP_MD_CTX * ctx ;
263
263
unsigned char buffer [4096 ];
264
264
struct stat st ;
265
265
off_t size ;
266
- int fd , bytes , i ;
266
+ int fd , bytes ;
267
+ unsigned int i , md_len ;
267
268
268
269
fd = open (filename , O_RDONLY );
269
270
if (fd < 0 )
@@ -273,7 +274,11 @@ md5_file(const char *filename, char *buf)
273
274
goto err ;
274
275
}
275
276
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
+ }
277
282
size = st .st_size ;
278
283
bytes = 0 ;
279
284
while (size > 0 ) {
@@ -283,7 +288,10 @@ md5_file(const char *filename, char *buf)
283
288
bytes = read (fd , buffer , size );
284
289
if (bytes < 0 )
285
290
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
+ }
287
295
size -= bytes ;
288
296
}
289
297
@@ -292,17 +300,23 @@ md5_file(const char *filename, char *buf)
292
300
if (bytes < 0 )
293
301
return NULL ;
294
302
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
+
295
310
if (!buf )
296
- buf = malloc (MD5_DIGEST_LENGTH * 2 + 1 );
311
+ buf = malloc (md_len * 2 + 1 );
297
312
if (!buf )
298
313
return NULL ;
299
314
300
- MD5_Final (digest , & ctx );
301
- for (i = 0 ; i < MD5_DIGEST_LENGTH ; i ++ ) {
315
+ for (i = 0 ; i < md_len ; i ++ ) {
302
316
buf [2 * i ] = hex [digest [i ] >> 4 ];
303
317
buf [2 * i + 1 ] = hex [digest [i ] & 0x0f ];
304
318
}
305
- buf [MD5_DIGEST_LENGTH * 2 ] = '\0' ;
319
+ buf [md_len * 2 ] = '\0' ;
306
320
307
321
return buf ;
308
322
}
0 commit comments