-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
132 lines (117 loc) · 3.26 KB
/
main.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include "./sha1sat/sha1sat.h"
#include "./sha2sat/sha256sat.h"
int main(int argc, char **argv) {
int res = 0;
char opt = 0;
FILE * stream = NULL;
int help = 0;
int kind = 0;
char * output = NULL;
char * digest = NULL;
ssize_t msize = -1;
const char * short_options = "ho:d:m:";
const struct option long_options[4] = {
{ "help", no_argument, &help, 1 },
{ "sha1", no_argument, &kind, 1 },
{ "sha256", no_argument, &kind, 256 },
{ "sha224", no_argument, &kind, 224 }
};
while (
(opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1
) {
switch (opt) {
case 'h':
help = 1;
break;
case 'o':
output = optarg;
stream = fopen(output, "w+");
if (stream == NULL) {
printf(
"Error: Failed to open or create a file at %s for writing"
". %s\n",
output, strerror(errno)
);
}
break;
case 'd':
digest = optarg;
break;
case 'm':
msize = (size_t)atoi(optarg);
break;
default:
break;
}
}
if (help) {
printf(
"Usage: ./shasat [options]\n"
"Options:\n"
" %-24s %s\n"
" %-24s %s\n"
" %-24s %s\n"
" %-24s %s\n"
" %-24s %s\n"
" %-24s %s\n"
" %-24s %s\n",
"--help",
"Display this information.",
"--sha1",
"Use SHA1 as the hash function.",
"--sha256",
"Use SHA256 as the hash function.",
"--sha224",
"Use SHA224 as the hash function.",
"-o <file>",
"Save the output Boolean expression in dimacs cnf form to <file>.",
"-d <digest>",
"Use <digest> as the output digest.",
"-m <message_size>",
"Use <message_size> as the size, in bytes, of the unknown input "
"message."
);
return 0;
}
if (kind == 0) {
printf("Error: No hash function specified.\n");
exit(EXIT_FAILURE);
}
if (output == NULL) {
printf("Error: No output file specified.\n");
exit(EXIT_FAILURE);
}
if (digest == NULL) {
printf("Error: No digest specified.\n");
exit(EXIT_FAILURE);
}
if (msize == -1) {
printf("Error: No message size specified.\n");
exit(EXIT_FAILURE);
}
switch (msize) {
case 1:
res = sha1sat(stream, msize, digest);
case 256:
res = sha256sat(stream, msize, digest);
case 224:
res = sha224sat(stream, msize, digest);
}
if (res < 0) {
printf(
"Error: Could not reduce SHA%d to SAT using the specified arguments.\n",
kind
);
exit(EXIT_FAILURE);
}
printf(
"Successfully reduced SHA%d to SAT.\n"
"Resultant Boolean expression outputted to %s.\n",
kind, output
);
return 0;
}