1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <gmssl/md5.h>
17 #include <gmssl/hex.h>
18
19
20 static char *teststr[] = {
21 "",
22 "a",
23 "abc",
24 "message digest",
25 "abcdefghijklmnopqrstuvwxyz",
26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
27 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
28 };
29
30 static char *dgsthex[] = {
31 "d41d8cd98f00b204e9800998ecf8427e",
32 "0cc175b9c0f1b6a831c399e269772661",
33 "900150983cd24fb0d6963f7d28e17f72",
34 "f96b697d7cb7938d525a2f31aaf161d0",
35 "c3fcd3d76192e4007dfb496cca67e13b",
36 "d174ab98d277d9f5a5611c2c9f419d9f",
37 "57edf4a22be3c955ac49da2e2107b67a",
38 };
39
main(int argc,char ** argv)40 int main(int argc, char **argv)
41 {
42 int err = 0;
43 char *p;
44 uint8_t dgst[16];
45 uint8_t dgstbuf[16];
46 size_t dgstbuflen;
47 size_t i;
48
49 for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
50 hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
51 md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst);
52
53 if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
54 int n;
55 printf("error calculating MD5 on %s\n", teststr[i]);
56 printf(" digest(corret) = ");
57 for (n = 0; n < sizeof(dgst); n++) {
58 printf("%02X", dgst[n]);
59 }
60 printf("\n");
61 printf(" digest(error) = %s\n", dgsthex[i]);
62 err++;
63 } else {
64 printf("md5 test %lu ok\n", i+1);
65 }
66 }
67
68 return err;
69 }
70