1f9f00f4a0fd0bf3a1a8840fe661a6f28e9d4a96 galt Wed Feb 13 15:11:47 2013 -0800 adding capability to run md5sum against an entire file diff --git src/lib/md5.c src/lib/md5.c index 4db7435..a2da909 100644 --- src/lib/md5.c +++ src/lib/md5.c @@ -1,23 +1,24 @@ /* * RFC 1321 compliant MD5 implementation, * by Christophe Devine <devine@cr0.net>; * this program is licensed under the GPL. */ #include "common.h" #include "md5.h" +#include "hex.h" #define GET_UINT32(n,b,i) \ { \ (n) = (uint32) ((uint8 *) b)[(i)] \ | (((uint32) ((uint8 *) b)[(i)+1]) << 8) \ | (((uint32) ((uint8 *) b)[(i)+2]) << 16) \ | (((uint32) ((uint8 *) b)[(i)+3]) << 24); \ } #define PUT_UINT32(n,b,i) \ { \ (((uint8 *) b)[(i)] ) = (uint8) (((n) ) & 0xFF); \ (((uint8 *) b)[(i)+1]) = (uint8) (((n) >> 8) & 0xFF); \ (((uint8 *) b)[(i)+2]) = (uint8) (((n) >> 16) & 0xFF); \ @@ -205,30 +206,73 @@ PUT_UINT32( (uint32) ((ctx->total << 3) & 0xFFFFFFFF), msglen, 0 ); PUT_UINT32( (uint32) ((ctx->total >> 29) & 0xFFFFFFFF), msglen, 4 ); last = (uint32) (ctx->total & 0x3F); padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); md5_update( ctx, md5_padding, padn ); md5_update( ctx, msglen, 8 ); PUT_UINT32( ctx->state[0], digest, 0 ); PUT_UINT32( ctx->state[1], digest, 4 ); PUT_UINT32( ctx->state[2], digest, 8 ); PUT_UINT32( ctx->state[3], digest, 12 ); } + +void md5ForFile(char * fileName, unsigned char md5[16]) +/* read f in buffer pieces and update md5 hash */ +{ +struct md5_context ctx; +unsigned char buffer[MD5READBUFSIZE]; +int bufRead = 0; +FILE *f = mustOpen(fileName,"rb"); + +md5_starts(&ctx); + +while ((bufRead = fread(&buffer, 1, MD5READBUFSIZE, f)) > 0) + { + md5_update(&ctx, buffer, bufRead); + } + +md5_finish(&ctx, md5); +carefulClose(&f); +} + +char *md5ToHex(unsigned char md5[16]) +/* return md5 as hex string */ +{ +char hex[33]; +char *h; +int i; +for (i = 0, h=hex; i < 16; ++i, ++h) + { + byteToHex( md5[i], h++); // note h is incremented here and also at the top of the loop + } +hex[32] = 0; +return cloneString(hex); +} + +char *md5HexForFile(char * fileName) +/* read f in buffer pieces and return hex string for md5sum */ +{ +// MD5 COMPUTE +unsigned char md5[16]; /* Keep the md5 checksum here. */ +md5ForFile(fileName,md5); +return md5ToHex(md5); +} + #ifdef TEST #include <stdio.h> /* * those are the standard RFC 1321 test vectors */ static char *msg[] = { "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz",