On Fri, 2018-09-21 at 19:35:53 +0200, Guillem Jover wrote: > On Sat, 2018-09-22 at 00:13:47 +0800, Yangfl wrote: > > Guillem Jover <guil...@debian.org> 于2018年9月21日周五 上午7:42写道: > > > Something like the attached patch would do I guess? I'm not sure if > > > I'd want to expose those unconditionally, as that might pollute the > > > namespace. > > > > Please find the attached md5main-md.c, taken from libmd5-rfc but > > modified to be compiled with -lmd. Notice the segfault here. Clearly > > MD5End supplies the hash in ASCII hex string, but md5_finish in raw > > char array. That's not the same. > > Ah indeed sorry, I used initially MD5Final() which is the correct one > here, but switched to MD5End() because it was failing the libmd test > suite. > > Patch updated, I'll fix the test suite later today.
The attached is what I'd include. Thanks, Guillem
diff --git i/include/md5.h w/include/md5.h index dee2bf4..2f8b167 100644 --- i/include/md5.h +++ w/include/md5.h @@ -47,4 +47,17 @@ char *MD5Data(const uint8_t *, size_t, char *); } #endif +/* + * Interface compatibility with Aladdin Enterprises independent + * implemntation from RFC 1321. + */ + +typedef uint8_t md5_byte_t; +typedef uint32_t md5_word_t; +typedef MD5_CTX md5_state_t; + +#define md5_init(pms) MD5Init(pms) +#define md5_append(pms, data, nbytes) MD5Update(pms, data, nbytes) +#define md5_finish(pms, digest) MD5Final(digest, pms) + #endif /* _MD5_H_ */ diff --git i/test/md5.c w/test/md5.c index 5dac6e1..f18f398 100644 --- i/test/md5.c +++ w/test/md5.c @@ -30,12 +30,55 @@ #include <md5.h> #include <string.h> +static int +hexchar2bin(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + else if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + assert(!"invalid hexadecimal input"); +} + +static void +hex2bin(uint8_t *bin, const char *str, size_t bin_len) +{ + int i; + + for (i = 0; i < bin_len; i++) + bin[i] = hexchar2bin(str[i * 2]) << 4 | + hexchar2bin(str[i * 2 + 1]); +} + void -test_md5(const char *digest, const char *string) +test_md5(const char *digest_str, const char *string) { - char result[MD5_DIGEST_STRING_LENGTH]; + uint8_t digest[MD5_DIGEST_LENGTH]; + uint8_t result[MD5_DIGEST_LENGTH]; + char result_str[MD5_DIGEST_STRING_LENGTH]; + MD5_CTX ctx; + md5_state_t pms; + + hex2bin(digest, digest_str, MD5_DIGEST_LENGTH); + + assert(strcmp(digest_str, MD5Data(string, strlen(string), result_str)) == 0); + + MD5Init(&ctx); + MD5Update(&ctx, string, strlen(string)); + MD5End(&ctx, result_str); + assert(strcmp(digest_str, result_str) == 0); + + MD5Init(&ctx); + MD5Update(&ctx, string, strlen(string)); + MD5Final(result, &ctx); + assert(memcmp(digest, result, MD5_DIGEST_LENGTH) == 0); - assert(strcmp(digest, MD5Data(string, strlen(string), result)) == 0); + md5_init(&pms); + md5_append(&pms, string, strlen(string)); + md5_finish(&pms, result); + assert(memcmp(digest, result, MD5_DIGEST_LENGTH) == 0); } int