diff options
Diffstat (limited to 'common/md5.c')
-rw-r--r-- | common/md5.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/common/md5.c b/common/md5.c index c909bb3..2f92bec 100644 --- a/common/md5.c +++ b/common/md5.c @@ -18,6 +18,8 @@ #include <string.h> #include "md5.h" +void md5_transform(unsigned int buf[4], unsigned int const in[16]); + #ifdef BIG_ENDIAN void byteSwap(unsigned int *buf, unsigned words) @@ -39,7 +41,7 @@ byteSwap(unsigned int *buf, unsigned words) * initialization constants. */ void -MD5Init(struct MD5Context *ctx) +md5_init(md5_ctx_t* ctx) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -55,9 +57,10 @@ MD5Init(struct MD5Context *ctx) * of bytes. */ void -MD5Update(struct MD5Context *ctx, const unsigned char* buf, unsigned len) +md5_update(md5_ctx_t* ctx, const void* b, unsigned len) { unsigned int t; + const unsigned char* buf = (const unsigned char*)b; /* Update byte count */ @@ -73,7 +76,7 @@ MD5Update(struct MD5Context *ctx, const unsigned char* buf, unsigned len) /* First chunk is an odd size */ memcpy((unsigned char *)ctx->in + 64 - t, buf, t); byteSwap(ctx->in, 16); - MD5Transform(ctx->buf, ctx->in); + md5_transform(ctx->buf, ctx->in); buf += t; len -= t; @@ -81,7 +84,7 @@ MD5Update(struct MD5Context *ctx, const unsigned char* buf, unsigned len) while (len >= 64) { memcpy(ctx->in, buf, 64); byteSwap(ctx->in, 16); - MD5Transform(ctx->buf, ctx->in); + md5_transform(ctx->buf, ctx->in); buf += 64; len -= 64; } @@ -95,7 +98,7 @@ MD5Update(struct MD5Context *ctx, const unsigned char* buf, unsigned len) * 1 0* (64-bit count of bits processed, MSB-first) */ void -MD5Final(unsigned char digest[16], struct MD5Context *ctx) +md5_final(unsigned char digest[16], md5_ctx_t* ctx) { int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ unsigned char *p = (unsigned char *)ctx->in + count; @@ -109,7 +112,7 @@ MD5Final(unsigned char digest[16], struct MD5Context *ctx) if (count < 0) { /* Padding forces an extra block */ memset(p, 0, (unsigned int)count + 8); byteSwap(ctx->in, 16); - MD5Transform(ctx->buf, ctx->in); + md5_transform(ctx->buf, ctx->in); p = (unsigned char *)ctx->in; count = 56; } @@ -119,13 +122,30 @@ MD5Final(unsigned char digest[16], struct MD5Context *ctx) /* Append length in bits and transform */ ctx->in[14] = ctx->bytes[0] << 3; ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; - MD5Transform(ctx->buf, ctx->in); + md5_transform(ctx->buf, ctx->in); byteSwap(ctx->buf, 4); memcpy(digest, ctx->buf, 16); memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ } +void +md5_string(unsigned char digest[MD5_LEN], const char* str) +{ + md5_ctx_t md5; + md5_init(&md5); + md5_update(&md5, str, strlen(str)); + md5_final(digest, &md5); +} + +int +md5_strcmp(unsigned char digest[MD5_LEN], const char* str) +{ + unsigned char other[MD5_LEN]; + md5_string(other, str); + return memcmp(digest, other, MD5_LEN); +} + /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ @@ -144,7 +164,7 @@ MD5Final(unsigned char digest[16], struct MD5Context *ctx) * the data and converts bytes into longwords for this routine. */ void -MD5Transform(unsigned int buf[4], unsigned int const in[16]) +md5_transform(unsigned int buf[4], unsigned int const in[16]) { register unsigned int a, b, c, d; |