aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: e0c382e6bac62d084bd64fa4241a8a207bf881dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/***
* Copyright 2017 Marc Stevens <marc@marc-stevens.nl>, Dan Shumow <danshu@microsoft.com>
* Distributed under the MIT Software License.
* See accompanying file LICENSE.txt or copy at
* https://opensource.org/licenses/MIT
***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>

#include "sha1.h"

int main(int argc, char** argv)
{
	FILE* fd;
	unsigned char hash2[20];
	char buffer[65536];
	size_t size;
	SHA1_CTX ctx2;
	int i,j;

	if (argc < 2)
	{
		printf("Usage: %s <file>\n", basename(argv[0]));
		return 1;
	}

	for (i=1; i < argc; ++i)
	{
		SHA1DCInit(&ctx2);

		/* if the program name includes the word 'partial' then also test for reduced-round SHA-1 collisions */
		if (NULL != strstr(argv[0], "partial"))
		{
			SHA1DCSetDetectReducedRoundCollision(&ctx2, 1);
		}

		fd = fopen(argv[i], "rb");
		if (fd == NULL)
		{
			printf("cannot open file: %s\n", argv[i]);
			return 1;
		}

		while (1)
		{
			size=fread(buffer,1,65536,fd);
			SHA1DCUpdate(&ctx2, buffer, (unsigned)(size));
			if (size != 65536)
				break;
		}
		if (ferror(fd))
		{
			printf("error while reading file: %s\n", argv[i]);
			return 1;
		}
		if (!feof(fd))
		{
			printf("not end of file?: %s\n",argv[i]);
			return 1;
		}

		SHA1DCFinal(hash2,&ctx2);

		for (j = 0; j < 20; ++j)
		{
			sprintf(buffer+(j*2), "%02x", hash2[j]);
		}
		buffer[20*2] = 0;
		if (ctx2.found_collision)
		{
			printf("%s *coll* %s\n", buffer, argv[i]);
		}
		else
		{
			printf("%s  %s\n", buffer, argv[i]);
		}

		fclose(fd);
	}
	return 0;
}