src.dualinventive.com/fw/libdi_fw-tests/libdi/tests/crypt_md5.c

82 lines
1.9 KiB
C

/**
* @file tests/crypt_md5.c
* Test md5 using rfc1321.txt
* @todo use test library and verify against expected digest
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <di/crypt/md5.h>
#include <di/misc/hexstr.h>
struct _crypt_md5_test {
const char *msg;
const char *digest;
} crypt_md5_test_list[] = {
{ .msg = "", .digest = "d41d8cd98f00b204e9800998ecf8427e" },
{ .msg = "a", .digest = "0cc175b9c0f1b6a831c399e269772661" },
{ .msg = "abc", .digest = "900150983cd24fb0d6963f7d28e17f72" },
{
.msg = "message_digest",
.digest = "f96b697d7cb7938d525a2f31aaf161d0"
},
{
.msg = "abcdefghijklmnopqrstuvwxyz",
.digest = "c3fcd3d76192e4007dfb496cca67e13b"
},
{
.msg = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
.digest = "d174ab98d277d9f5a5611c2c9f419d9f"
},
{
.msg = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
.digest = "57edf4a22be3c955ac49da2e2107b67a"
},
{ .msg = NULL, .digest = NULL }
};
void di_crypt_md5_print_digest(const uint8_t *digest, size_t len)
{
for (size_t i = 0; i < len; i++)
printf("%02x", digest[i]);
printf("\n");
}
void di_crypt_md5_test(
const char *descr, unsigned char tcase,
const char *msg, const char *digest)
{
uint8_t digest_out[16];
char digest_out_str[33];
printf("=== md5 %s case #%u ===\n", descr, tcase);
printf("msg: \"%s\"\n", msg);
printf("md5_exp: 0x%s\n", digest);
di_crypt_md5(msg, strlen(msg), digest_out);
di_hexstr_from_bin(digest_out_str, 33, digest_out, 16);
printf("md5_got: 0x%s\n", digest_out_str);
}
void di_crypt_md5_rfc1321(void)
{
const char *descr = "rfc1321";
unsigned int tcase = 1;
struct _crypt_md5_test *t = &crypt_md5_test_list[0];
while (t->msg != NULL && t->digest != NULL) {
di_crypt_md5_test(descr, tcase, t->msg, t->digest);
tcase++;
t++;
}
}
int
main(void)
{
di_crypt_md5_rfc1321();
return 0;
}