annotate Resources/sha1/shacmp.cpp @ 265:9cd240cfd3a6 Orthanc-0.3.1

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