ce188d4d |
1 | /********************************************************************* |
2 | * Filename: md5_test.c |
3 | * Author: Brad Conte (brad AT bradconte.com) |
4 | * Copyright: |
5 | * Disclaimer: This code is presented "as is" without any guarantees. |
6 | * Details: Performs known-answer tests on the corresponding MD5 |
7 | implementation. These tests do not encompass the full |
8 | range of available test vectors, however, if the tests |
9 | pass it is very, very likely that the code is correct |
10 | and was compiled properly. This code also serves as |
11 | example usage of the functions. |
12 | *********************************************************************/ |
13 | |
14 | /*************************** HEADER FILES ***************************/ |
15 | #include <stdio.h> |
16 | #include <memory.h> |
17 | #include <string.h> |
18 | #include "md5.h" |
19 | |
20 | /*********************** FUNCTION DEFINITIONS ***********************/ |
21 | int md5_test() |
22 | { |
23 | BYTE text1[] = {""}; |
24 | BYTE text2[] = {"abc"}; |
25 | BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"}; |
26 | BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"}; |
27 | BYTE hash1[MD5_BLOCK_SIZE] = {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e}; |
28 | BYTE hash2[MD5_BLOCK_SIZE] = {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72}; |
29 | BYTE hash3[MD5_BLOCK_SIZE] = {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f}; |
30 | BYTE buf[16]; |
31 | MD5_CTX ctx; |
32 | int pass = 1; |
33 | |
34 | md5_init(&ctx); |
35 | md5_update(&ctx, text1, strlen(text1)); |
36 | md5_final(&ctx, buf); |
37 | pass = pass && !memcmp(hash1, buf, MD5_BLOCK_SIZE); |
38 | |
39 | // Note the MD5 object can be reused. |
40 | md5_init(&ctx); |
41 | md5_update(&ctx, text2, strlen(text2)); |
42 | md5_final(&ctx, buf); |
43 | pass = pass && !memcmp(hash2, buf, MD5_BLOCK_SIZE); |
44 | |
45 | // Note the data is being added in two chunks. |
46 | md5_init(&ctx); |
47 | md5_update(&ctx, text3_1, strlen(text3_1)); |
48 | md5_update(&ctx, text3_2, strlen(text3_2)); |
49 | md5_final(&ctx, buf); |
50 | pass = pass && !memcmp(hash3, buf, MD5_BLOCK_SIZE); |
51 | |
52 | return(pass); |
53 | } |
54 | |
55 | int main() |
56 | { |
57 | printf("MD5 tests: %s\n", md5_test() ? "SUCCEEDED" : "FAILED"); |
58 | |
59 | return(0); |
60 | } |