]> WPIA git - cassiopeia.git/blob - lib/collissiondetect/src/main.c
9bb6c8c06ab60061ea85678abfc1c04faf2577e8
[cassiopeia.git] / lib / collissiondetect / src / main.c
1 /**************************************************************************\
2 |
3 |    Copyright (C) 2012 CWI
4 |    
5 |    Contact:
6 |    Marc Stevens
7 |    Cryptology Group
8 |    Centrum Wiskunde & Informatica
9 |    P.O. Box 94079, 1090 GB Amsterdam, Netherlands
10 |    marc@marc-stevens.nl
11 |
12 |  Permission is hereby granted, free of charge, to any person obtaining a copy
13 |  of this software and associated documentation files (the "Software"), to deal
14 |  in the Software without restriction, including without limitation the rights
15 |  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 |  copies of the Software, and to permit persons to whom the Software is
17 |  furnished to do so, subject to the following conditions:
18
19 |  The above copyright notice and this permission notice shall be included in
20 |  all copies or substantial portions of the Software.
21
22 |  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 |  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 |  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 |  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 |  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 |  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 |  THE SOFTWARE.
29 |
30 \**************************************************************************/
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "libdetectcoll.h"
37
38 int main(int argc, char** argv) {
39         FILE* fd;
40         unsigned char hash[16];
41         unsigned char hash2[20];
42         char buffer[65536];
43         unsigned size;
44         MD5_CTX ctx;
45         SHA1_CTX ctx2;
46         int i,j;
47
48         gen_sha1_dv_xm_tables();
49         if (argc < 2) {
50                 printf("Usage: md5sum <file>\n");
51                 return 1;
52         }
53         for (i=1; i < argc; ++i) {
54                 fd = fopen(argv[i], "rb");
55                 if (fd == NULL) {
56                         printf("cannot open file: %s\n", argv[i]);
57                         return 1;
58                 }
59
60                 MD5Init_unsafe(&ctx);
61                 SHA1Init_unsafe(&ctx2);
62
63                 while (1) {
64                         size=fread(buffer,1,65536,fd);
65                         MD5Update(&ctx, buffer, size);
66                         SHA1Update(&ctx2, buffer, size);
67                         if (size != 65536)
68                                 break;
69                 }
70                 if (ferror(fd)) {
71                         printf("error while reading file: %s\n", argv[i]);
72                         return 1;
73                 }
74                 if (!feof(fd)) {
75                         printf("not end of file?: %s\n",argv[i]);
76                         return 1;
77                 }
78
79                 MD5Final(hash,&ctx);
80                 for (j = 0; j < 16; ++j) 
81                         sprintf(buffer+(j*2), "%02x", hash[j]);
82                 buffer[32] = 0;
83                 if (ctx.found_collision) {
84                         printf("md5 *coll* %s %s\n", buffer, argv[i]);
85                 } else {
86                         printf("md5 %s %s\n", buffer, argv[i]);
87                 }
88
89                 SHA1Final(hash2,&ctx2);
90                 for (j = 0; j < 20; ++j) 
91                         sprintf(buffer+(j*2), "%02x", hash2[j]);
92                 buffer[20*2] = 0;
93                 if (ctx2.found_collision) {
94                         printf("sha1 *coll* %s %s\n", buffer, argv[i]);
95                 } else {
96                         printf("sha1 %s %s\n", buffer, argv[i]);
97                 }
98                 printf("\n");
99
100                 fclose(fd);
101         }
102 }