2 * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #endif /* STDC_HEADERS */
29 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
33 #endif /* HAVE_STRING_H */
36 #endif /* HAVE_STRINGS_H */
37 #if defined(HAVE_STDINT_H)
39 #elif defined(HAVE_INTTYPES_H)
40 # include <inttypes.h>
46 __dso_public int main(int argc, char *argv[]);
48 static struct digest_function {
49 const char *digest_name;
51 void (*init)(SHA2_CTX *);
52 void (*update)(SHA2_CTX *, const unsigned char *, size_t);
53 void (*final)(unsigned char *, SHA2_CTX *);
54 } digest_functions[] = {
85 main(int argc, char *argv[])
89 struct digest_function *func;
90 unsigned char digest[SHA512_DIGEST_LENGTH];
91 static const char hex[] = "0123456789abcdef";
92 unsigned char buf[1000];
93 unsigned const char *test_strings[] = {
98 "abcdefghijklmnopqrstuvwxyz",
99 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
100 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
102 "12345678901234567890123456789012345678901234567890123456789"
103 "012345678901234567890",
106 for (func = digest_functions; func->digest_name != NULL; func++) {
107 for (i = 0; i < 8; i++) {
109 func->update(&ctx, test_strings[i], strlen(test_strings[i]));
110 func->final(digest, &ctx);
111 printf("%s (\"%s\") = ", func->digest_name, test_strings[i]);
112 for (j = 0; j < func->digest_len; j++) {
113 putchar(hex[digest[j] >> 4]);
114 putchar(hex[digest[j] & 0x0f]);
119 /* Simulate a string of a million 'a' characters. */
120 memset(buf, 'a', sizeof(buf));
122 for (i = 0; i < 1000; i++) {
123 func->update(&ctx, buf, sizeof(buf));
125 func->final(digest, &ctx);
126 printf("%s (one million 'a' characters) = ", func->digest_name);
127 for (j = 0; j < func->digest_len; j++) {
128 putchar(hex[digest[j] >> 4]);
129 putchar(hex[digest[j] & 0x0f]);