Welcome to mirror list, hosted at ThFree Co, Russian Federation.

main.c « src - github.com/cr-marcstevens/sha1collisiondetection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5948303b348c7debe33279348337d9a1350f5a0 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/***
* 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 <errno.h>
#ifndef _WIN32
#include <libgen.h>
#endif

#include "sha1.h"

#ifdef _WIN32
char* basename(char* path)
{
    char *base = NULL, *cur = NULL;

    base = path;
    cur = path;
    while (0 != *cur)
    {
        if ('\\' == *cur)
        {
            base = cur + 1;
        }
        cur++;
    }

    return base;
}
#endif

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

	if (argc < 2)
	{
		fprintf(stderr, "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);
		}

		if(!strcmp(argv[i],"-")) {
			fd = stdin;
		} else {
			fd = fopen(argv[i], "rb");
		}
		if (fd == NULL)
		{
			fprintf(stderr, "cannot open file: %s: %s\n", argv[i], strerror(errno));
			return 1;
		}

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

		foundcollision = SHA1DCFinal(hash2,&ctx2);

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

		fclose(fd);
	}
	return 0;
}

#ifdef _MSC_VER
#pragma warning(disable : 4710 )    /* 4710 -- compiler complains about printf,sprintf not being inlined. */
#endif