177
|
1 /*
|
|
2 * sha.cpp
|
|
3 *
|
|
4 * Copyright (C) 1998, 2009
|
|
5 * Paul E. Jones <paulej@packetizer.com>
|
|
6 * All Rights Reserved
|
|
7 *
|
|
8 *****************************************************************************
|
|
9 * $Id: sha.cpp 13 2009-06-22 20:20:32Z paulej $
|
|
10 *****************************************************************************
|
|
11 *
|
|
12 * Description:
|
|
13 * This utility will display the message digest (fingerprint) for
|
|
14 * the specified file(s).
|
|
15 *
|
|
16 * Portability Issues:
|
|
17 * None.
|
|
18 */
|
|
19
|
|
20 #include <stdio.h>
|
|
21 #include <string.h>
|
|
22 #ifdef WIN32
|
|
23 #include <io.h>
|
|
24 #endif
|
|
25 #include <fcntl.h>
|
|
26 #include "sha1.h"
|
|
27
|
|
28 /*
|
|
29 * Function prototype
|
|
30 */
|
|
31 void usage();
|
|
32
|
|
33
|
|
34 /*
|
|
35 * main
|
|
36 *
|
|
37 * Description:
|
|
38 * This is the entry point for the program
|
|
39 *
|
|
40 * Parameters:
|
|
41 * argc: [in]
|
|
42 * This is the count of arguments in the argv array
|
|
43 * argv: [in]
|
|
44 * This is an array of filenames for which to compute message digests
|
|
45 *
|
|
46 * Returns:
|
|
47 * Nothing.
|
|
48 *
|
|
49 * Comments:
|
|
50 *
|
|
51 */
|
|
52 int main(int argc, char *argv[])
|
|
53 {
|
|
54 SHA1 sha; // SHA-1 class
|
|
55 FILE *fp; // File pointer for reading files
|
|
56 char c; // Character read from file
|
|
57 unsigned message_digest[5]; // Message digest from "sha"
|
|
58 int i; // Counter
|
|
59 bool reading_stdin; // Are we reading standard in?
|
|
60 bool read_stdin = false; // Have we read stdin?
|
|
61
|
|
62 /*
|
|
63 * Check the program arguments and print usage information if -?
|
|
64 * or --help is passed as the first argument.
|
|
65 */
|
|
66 if (argc > 1 && (!strcmp(argv[1],"-?") || !strcmp(argv[1],"--help")))
|
|
67 {
|
|
68 usage();
|
|
69 return 1;
|
|
70 }
|
|
71
|
|
72 /*
|
|
73 * For each filename passed in on the command line, calculate the
|
|
74 * SHA-1 value and display it.
|
|
75 */
|
|
76 for(i = 0; i < argc; i++)
|
|
77 {
|
|
78 /*
|
|
79 * We start the counter at 0 to guarantee entry into the for loop.
|
|
80 * So if 'i' is zero, we will increment it now. If there is no
|
|
81 * argv[1], we will use STDIN below.
|
|
82 */
|
|
83 if (i == 0)
|
|
84 {
|
|
85 i++;
|
|
86 }
|
|
87
|
|
88 if (argc == 1 || !strcmp(argv[i],"-"))
|
|
89 {
|
|
90 #ifdef WIN32
|
|
91 _setmode(_fileno(stdin), _O_BINARY);
|
|
92 #endif
|
|
93 fp = stdin;
|
|
94 reading_stdin = true;
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 if (!(fp = fopen(argv[i],"rb")))
|
|
99 {
|
|
100 fprintf(stderr, "sha: unable to open file %s\n", argv[i]);
|
|
101 return 2;
|
|
102 }
|
|
103 reading_stdin = false;
|
|
104 }
|
|
105
|
|
106 /*
|
|
107 * We do not want to read STDIN multiple times
|
|
108 */
|
|
109 if (reading_stdin)
|
|
110 {
|
|
111 if (read_stdin)
|
|
112 {
|
|
113 continue;
|
|
114 }
|
|
115
|
|
116 read_stdin = true;
|
|
117 }
|
|
118
|
|
119 /*
|
|
120 * Reset the SHA1 object and process input
|
|
121 */
|
|
122 sha.Reset();
|
|
123
|
|
124 c = fgetc(fp);
|
|
125 while(!feof(fp))
|
|
126 {
|
|
127 sha.Input(c);
|
|
128 c = fgetc(fp);
|
|
129 }
|
|
130
|
|
131 if (!reading_stdin)
|
|
132 {
|
|
133 fclose(fp);
|
|
134 }
|
|
135
|
|
136 if (!sha.Result(message_digest))
|
|
137 {
|
|
138 fprintf(stderr,"sha: could not compute message digest for %s\n",
|
|
139 reading_stdin?"STDIN":argv[i]);
|
|
140 }
|
|
141 else
|
|
142 {
|
|
143 printf( "%08X %08X %08X %08X %08X - %s\n",
|
|
144 message_digest[0],
|
|
145 message_digest[1],
|
|
146 message_digest[2],
|
|
147 message_digest[3],
|
|
148 message_digest[4],
|
|
149 reading_stdin?"STDIN":argv[i]);
|
|
150 }
|
|
151 }
|
|
152
|
|
153 return 0;
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * usage
|
|
158 *
|
|
159 * Description:
|
|
160 * This function will display program usage information to the user.
|
|
161 *
|
|
162 * Parameters:
|
|
163 * None.
|
|
164 *
|
|
165 * Returns:
|
|
166 * Nothing.
|
|
167 *
|
|
168 * Comments:
|
|
169 *
|
|
170 */
|
|
171 void usage()
|
|
172 {
|
|
173 printf("usage: sha <file> [<file> ...]\n");
|
|
174 printf("\tThis program will display the message digest (fingerprint)\n");
|
|
175 printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n");
|
|
176 }
|