3 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
5 * Copyright (c) 2000-2001, Aaron D. Gifford
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: sha2prog.c,v 1.1 2001/11/08 00:02:11 adg Exp adg $
37 #include <sys/types.h>
43 void usage(char *prog, char *msg) {
44 fprintf(stderr, "%s\nUsage:\t%s [options] [<file>]\nOptions:\n\t-256\tGenerate SHA-256 hash\n\t-384\tGenerate SHA-284 hash\n\t-512\tGenerate SHA-512 hash\n\t-ALL\tGenerate all three hashes\n\t-q\tQuiet mode - only output hexadecimal hashes, one per line\n\n", msg, prog);
50 int main(int argc, char **argv) {
52 int quiet = 0, hash = 0;
53 char *av, *file = (char*)0;
58 unsigned char buf[BUFLEN];
64 /* Read data from STDIN by default */
69 if (*argv[ac] == '-') {
71 if (!strcmp(av, "q")) {
73 } else if (!strcmp(av, "256")) {
75 } else if (!strcmp(av, "384")) {
77 } else if (!strcmp(av, "512")) {
79 } else if (!strcmp(av, "ALL")) {
82 usage(argv[0], "Invalid option.");
88 usage(argv[0], "Too many arguments.");
90 if ((IN = fopen(file, "r")) == NULL) {
98 hash = 7; /* Default to ALL */
101 while ((l = read(fd,buf,BUFLEN)) > 0) {
103 SHA256_Update(&ctx256, (unsigned char*)buf, l);
104 SHA384_Update(&ctx384, (unsigned char*)buf, l);
105 SHA512_Update(&ctx512, (unsigned char*)buf, l);
112 SHA256_End(&ctx256, buf);
114 printf("SHA-256 (%s) = ", file);
118 SHA384_End(&ctx384, buf);
120 printf("SHA-384 (%s) = ", file);
124 SHA512_End(&ctx512, buf);
126 printf("SHA-512 (%s) = ", file);