1 /*
2  * FILE:	sha2prog.c
3  * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
4  *
5  * Copyright (c) 2000-2001, Aaron D. Gifford
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $Id: sha2prog.c,v 1.1 2001/11/08 00:02:11 adg Exp adg $
33  */
34 
35 #include <stdio.h>
36 #include <sysexits.h>
37 #include <sys/types.h>
38 #include <sys/uio.h>
39 #include <unistd.h>
40 
41 #include "sha2.h"
42 
usage(char * prog,char * msg)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);
45 	exit(-1);
46 }
47 
48 #define BUFLEN 16384
49 
main(int argc,char ** argv)50 int main(int argc, char **argv) {
51 	int		kl, l, fd, ac;
52 	int		quiet = 0, hash = 0;
53 	char		*av, *file = (char*)0;
54 	FILE		*IN = (FILE*)0;
55 	SHA256_CTX	ctx256;
56 	SHA384_CTX	ctx384;
57 	SHA512_CTX	ctx512;
58 	unsigned char	buf[BUFLEN];
59 
60 	SHA256_Init(&ctx256);
61 	SHA384_Init(&ctx384);
62 	SHA512_Init(&ctx512);
63 
64 	/* Read data from STDIN by default */
65 	fd = fileno(stdin);
66 
67 	ac = 1;
68 	while (ac < argc) {
69 		if (*argv[ac] == '-') {
70 			av = argv[ac] + 1;
71 			if (!strcmp(av, "q")) {
72 				quiet = 1;
73 			} else if (!strcmp(av, "256")) {
74 				hash |= 1;
75 			} else if (!strcmp(av, "384")) {
76 				hash |= 2;
77 			} else if (!strcmp(av, "512")) {
78 				hash |= 4;
79 			} else if (!strcmp(av, "ALL")) {
80 				hash = 7;
81 			} else {
82 				usage(argv[0], "Invalid option.");
83 			}
84 			ac++;
85 		} else {
86 			file = argv[ac++];
87 			if (ac != argc) {
88 				usage(argv[0], "Too many arguments.");
89 			}
90 			if ((IN = fopen(file, "r")) == NULL) {
91 				perror(argv[0]);
92 				exit(-1);
93 			}
94 			fd = fileno(IN);
95 		}
96 	}
97 	if (hash == 0)
98 		hash = 7;	/* Default to ALL */
99 
100 	kl = 0;
101 	while ((l = read(fd,buf,BUFLEN)) > 0) {
102 		kl += l;
103 		SHA256_Update(&ctx256, (unsigned char*)buf, l);
104 		SHA384_Update(&ctx384, (unsigned char*)buf, l);
105 		SHA512_Update(&ctx512, (unsigned char*)buf, l);
106 	}
107 	if (file) {
108 		fclose(IN);
109 	}
110 
111 	if (hash & 1) {
112 		SHA256_End(&ctx256, buf);
113 		if (!quiet)
114 			printf("SHA-256 (%s) = ", file);
115 		printf("%s\n", buf);
116 	}
117 	if (hash & 2) {
118 		SHA384_End(&ctx384, buf);
119 		if (!quiet)
120 			printf("SHA-384 (%s) = ", file);
121 		printf("%s\n", buf);
122 	}
123 	if (hash & 4) {
124 		SHA512_End(&ctx512, buf);
125 		if (!quiet)
126 			printf("SHA-512 (%s) = ", file);
127 		printf("%s\n", buf);
128 	}
129 
130 	return 1;
131 }
132 
133