Mercurial > hg > orthanc
comparison Resources/sha1/shacmp.cpp @ 177:81b6f3013738
sha1
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Nov 2012 10:42:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
176:81f11fb357f2 | 177:81b6f3013738 |
---|---|
1 /* | |
2 * shacmp.cpp | |
3 * | |
4 * Copyright (C) 1998, 2009 | |
5 * Paul E. Jones <paulej@packetizer.com> | |
6 * All Rights Reserved | |
7 * | |
8 ***************************************************************************** | |
9 * $Id: shacmp.cpp 12 2009-06-22 19:34:25Z paulej $ | |
10 ***************************************************************************** | |
11 * | |
12 * Description: | |
13 * This utility will compare two files by producing a message digest | |
14 * for each file using the Secure Hashing Algorithm and comparing | |
15 * the message digests. This function will return 0 if they | |
16 * compare or 1 if they do not or if there is an error. | |
17 * Errors result in a return code higher than 1. | |
18 * | |
19 * Portability Issues: | |
20 * none. | |
21 * | |
22 */ | |
23 | |
24 #include <stdio.h> | |
25 #include <string.h> | |
26 #include "sha1.h" | |
27 | |
28 /* | |
29 * Return codes | |
30 */ | |
31 #define SHA1_COMPARE 0 | |
32 #define SHA1_NO_COMPARE 1 | |
33 #define SHA1_USAGE_ERROR 2 | |
34 #define SHA1_FILE_ERROR 3 | |
35 | |
36 /* | |
37 * Function prototype | |
38 */ | |
39 void usage(); | |
40 | |
41 /* | |
42 * main | |
43 * | |
44 * Description: | |
45 * This is the entry point for the program | |
46 * | |
47 * Parameters: | |
48 * argc: [in] | |
49 * This is the count of arguments in the argv array | |
50 * argv: [in] | |
51 * This is an array of filenames for which to compute message digests | |
52 * | |
53 * Returns: | |
54 * Nothing. | |
55 * | |
56 * Comments: | |
57 * | |
58 */ | |
59 int main(int argc, char *argv[]) | |
60 { | |
61 SHA1 sha; // SHA-1 class | |
62 FILE *fp; // File pointer for reading files | |
63 char c; // Character read from file | |
64 unsigned message_digest[2][5]; // Message digest for files | |
65 int i; // Counter | |
66 bool message_match; // Message digest match flag | |
67 int returncode; | |
68 | |
69 /* | |
70 * If we have two arguments, we will assume they are filenames. If | |
71 * we do not have to arguments, call usage() and exit. | |
72 */ | |
73 if (argc != 3) | |
74 { | |
75 usage(); | |
76 return SHA1_USAGE_ERROR; | |
77 } | |
78 | |
79 /* | |
80 * Get the message digests for each file | |
81 */ | |
82 for(i = 1; i <= 2; i++) | |
83 { | |
84 sha.Reset(); | |
85 | |
86 if (!(fp = fopen(argv[i],"rb"))) | |
87 { | |
88 fprintf(stderr, "sha: unable to open file %s\n", argv[i]); | |
89 return SHA1_FILE_ERROR; | |
90 } | |
91 | |
92 c = fgetc(fp); | |
93 while(!feof(fp)) | |
94 { | |
95 sha.Input(c); | |
96 c = fgetc(fp); | |
97 } | |
98 | |
99 fclose(fp); | |
100 | |
101 if (!sha.Result(message_digest[i-1])) | |
102 { | |
103 fprintf(stderr,"shacmp: could not compute message digest for %s\n", | |
104 argv[i]); | |
105 return SHA1_FILE_ERROR; | |
106 } | |
107 } | |
108 | |
109 /* | |
110 * Compare the message digest values | |
111 */ | |
112 message_match = true; | |
113 for(i = 0; i < 5; i++) | |
114 { | |
115 if (message_digest[0][i] != message_digest[1][i]) | |
116 { | |
117 message_match = false; | |
118 break; | |
119 } | |
120 } | |
121 | |
122 if (message_match) | |
123 { | |
124 printf("Fingerprints match:\n"); | |
125 returncode = SHA1_COMPARE; | |
126 } | |
127 else | |
128 { | |
129 printf("Fingerprints do not match:\n"); | |
130 returncode = SHA1_NO_COMPARE; | |
131 } | |
132 | |
133 printf( "\t%08X %08X %08X %08X %08X\n", | |
134 message_digest[0][0], | |
135 message_digest[0][1], | |
136 message_digest[0][2], | |
137 message_digest[0][3], | |
138 message_digest[0][4]); | |
139 printf( "\t%08X %08X %08X %08X %08X\n", | |
140 message_digest[1][0], | |
141 message_digest[1][1], | |
142 message_digest[1][2], | |
143 message_digest[1][3], | |
144 message_digest[1][4]); | |
145 | |
146 return returncode; | |
147 } | |
148 | |
149 /* | |
150 * usage | |
151 * | |
152 * Description: | |
153 * This function will display program usage information to the user. | |
154 * | |
155 * Parameters: | |
156 * None. | |
157 * | |
158 * Returns: | |
159 * Nothing. | |
160 * | |
161 * Comments: | |
162 * | |
163 */ | |
164 void usage() | |
165 { | |
166 printf("usage: shacmp <file> <file>\n"); | |
167 printf("\tThis program will compare the message digests (fingerprints)\n"); | |
168 printf("\tfor two files using the Secure Hashing Algorithm (SHA-1).\n"); | |
169 } |