Fix GCC build; make update; fix number re-use
[openssl.git] / test / md5test.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "../e_os.h"
15
16 #ifdef OPENSSL_NO_MD5
17 int main(int argc, char *argv[])
18 {
19     printf("No MD5 support\n");
20     return (0);
21 }
22 #else
23 # include <openssl/evp.h>
24 # include <openssl/md5.h>
25
26 static char *test[] = {
27     "",
28     "a",
29     "abc",
30     "message digest",
31     "abcdefghijklmnopqrstuvwxyz",
32     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
33     "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
34     NULL,
35 };
36
37 static char *ret[] = {
38     "d41d8cd98f00b204e9800998ecf8427e",
39     "0cc175b9c0f1b6a831c399e269772661",
40     "900150983cd24fb0d6963f7d28e17f72",
41     "f96b697d7cb7938d525a2f31aaf161d0",
42     "c3fcd3d76192e4007dfb496cca67e13b",
43     "d174ab98d277d9f5a5611c2c9f419d9f",
44     "57edf4a22be3c955ac49da2e2107b67a",
45 };
46
47 static char *pt(unsigned char *md);
48 int main(int argc, char *argv[])
49 {
50     int i, err = 0;
51     char **P, **R;
52     char *p;
53     unsigned char md[MD5_DIGEST_LENGTH];
54
55     P = test;
56     R = ret;
57     i = 1;
58     while (*P != NULL) {
59         EVP_Digest(&(P[0][0]), strlen((char *)*P), md, NULL, EVP_md5(), NULL);
60         p = pt(md);
61         if (strcmp(p, (char *)*R) != 0) {
62             printf("error calculating MD5 on '%s'\n", *P);
63             printf("got %s instead of %s\n", p, *R);
64             err++;
65         } else
66             printf("test %d ok\n", i);
67         i++;
68         R++;
69         P++;
70     }
71
72     EXIT(err);
73 }
74
75 static char *pt(unsigned char *md)
76 {
77     int i;
78     static char buf[80];
79
80     for (i = 0; i < MD5_DIGEST_LENGTH; i++)
81         sprintf(&(buf[i * 2]), "%02x", md[i]);
82     return (buf);
83 }
84 #endif