annotate Resources/sha1/sha.cpp @ 344:cd6749e53a03

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