-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.c
190 lines (154 loc) · 5.12 KB
/
build.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
#include "index.h"
#include "hash.h"
#include "defines.h"
void showProgress(uint64_t offset, uint64_t maxOffset, uint64_t hashesGenerated)
{
float percents = (float) offset / (float) maxOffset * 100;
printf("\033[A\r\33[2K%lu / %lu (%.2f%%) - %lu hashes generated\n", offset, maxOffset, percents, hashesGenerated);
}
int main(int argc, char** argv)
{
HashInfos hashInfos;
FILE* wordlistFile = NULL, *outputFile = NULL, *tmpFile = NULL;
uint8_t* digest = NULL;
uint8_t* copyBuffer = malloc(MIB);
char line[MAX_LINE_SIZE] = {0};
uint8_t compressedLine[MAX_LINE_SIZE] = {0};
char* tmp;
size_t lineLength, indexDataBits, indexDataBytes;
uint64_t wordlistFileSize, offset, i = 0;
uint32_t readSize;
uint64_t wordlistOffset;
WordType wordType;
uint32_t compressedBitsSize;
hashInfos.f = NULL;
if(argc != 6)
{
printf("Usage: %s <hash_function> <index_data_bits> <wordlist_file> <output_file> <tmp_file>\n", argv[0]);
return EXIT_FAILURE;
}
getHashInfos(argv[1], &hashInfos);
if(hashInfos.f == NULL)
{
printf("Hash name %s is not recognized. Supported hashes are: md5, sha1, sha256.\n", argv[1]);
return EXIT_FAILURE;
}
wordlistFile = fopen(argv[3], "r");
if(wordlistFile == NULL)
{
printf("Unable to open the wordlist file.\n");
return EXIT_FAILURE;
}
outputFile = fopen(argv[4], "w");
if(outputFile == NULL)
{
printf("Unable to open the output file.\n");
return EXIT_FAILURE;
}
tmpFile = fopen(argv[5], "w+");
if(tmpFile == NULL)
{
printf("Unable to open the temporary file.\n");
return EXIT_FAILURE;
}
wordlistFileSize = getFileSize(wordlistFile);
indexDataBits = strtol(argv[2], NULL, 10);
indexDataBytes = BYTES_SIZE(indexDataBits);
if(!isDataSizeValid(wordlistFile, indexDataBits))
{
printf("Invalid data size.\n");
return EXIT_FAILURE;
}
digest = malloc(hashInfos.digestSize);
// This header is only a placeholder for now
writeIndexHeader(outputFile, argv[1], indexDataBytes, 0);
while(fgets(line, MAX_LINE_SIZE, wordlistFile) != NULL)
{
tmp = memchr(line, '\r', MAX_LINE_SIZE);
if(tmp == NULL)
{
tmp = memchr(line, '\n', MAX_LINE_SIZE);
}
if(tmp == NULL)
{
printf("Error: the line is too long (larger than %u characters).\n", MAX_LINE_SIZE - 1);
return EXIT_FAILURE;
}
*tmp = '\0';
lineLength = tmp - line;
hashInfos.f((uint8_t*) line, lineLength, digest);
if(isNumeric(line))
{
compressNumeric(line, lineLength, compressedLine);
wordType = NUMERIC;
compressedBitsSize = NUMERIC_COMPRESSED_BITS(lineLength) + NUMERIC_SYMBOL_BITS;
}
else if(isAlphanumeric(line))
{
compressAlphanumeric(line, lineLength, compressedLine);
wordType = ALPHANUMERIC;
compressedBitsSize = ALPHANUMERIC_COMPRESSED_BITS(lineLength) + ALPHANUMERIC_SYMBOL_BITS;
}
else if(isReducedASCII(line))
{
compressReducedASCII(line, lineLength, compressedLine);
wordType = REDUCED_ASCII;
compressedBitsSize = REDUCED_ASCII_COMPRESSED_BITS(lineLength) + REDUCED_ASCII_SYMBOL_BITS;
}
else
{
wordType = NO_COMPRESSION;
compressedBitsSize = (lineLength + 1) << 3;
}
if(compressedBitsSize + 3 <= indexDataBits)
{
if(wordType == NO_COMPRESSION)
{
writeIndexEntryInline(digest, (uint8_t*) line, compressedBitsSize, indexDataBytes, NO_COMPRESSION, outputFile);
}
else
{
writeIndexEntryInline(digest, compressedLine, compressedBitsSize, indexDataBytes, wordType, outputFile);
}
}
else
{
writeIndexEntryPointer(digest, ftell(tmpFile), indexDataBytes, wordType, outputFile);
if(wordType == NO_COMPRESSION)
{
fwrite(line, sizeof(char), BYTES_SIZE(compressedBitsSize), tmpFile);
}
else
{
fwrite(compressedLine, sizeof(uint8_t), BYTES_SIZE(compressedBitsSize), tmpFile);
}
}
memset(line, '\0', MAX_LINE_SIZE);
offset = ftell(wordlistFile);
i++;
if((i % PROGRESS_UPDATE_COUNT) == 0)
{
showProgress(offset, wordlistFileSize, i);
}
}
wordlistOffset = ftell(outputFile) - sizeof(IndexHeader);
rewind(tmpFile);
while((readSize = fread(copyBuffer, 1, MIB, tmpFile)) != 0)
{
fwrite(copyBuffer, readSize, 1, outputFile);
}
rewind(outputFile);
writeIndexHeader(outputFile, argv[1], indexDataBytes, wordlistOffset);
free(copyBuffer);
free(digest);
fclose(wordlistFile);
fclose(outputFile);
fclose(tmpFile);
unlink(argv[5]);
return EXIT_SUCCESS;
}