-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-hash.c
318 lines (265 loc) · 8.06 KB
/
find-hash.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* compile with:
* clang -std=c89 -lcrypto find-hash.c -o find-hash
* Run with:
* ./find-hash wordlist
*
* printout stout yawls
* ty outlaws printouts
* wu lisp not statutory
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
/* TODO: HASH_SIZE should be only characters in the anagram not the whole alphabet */
#define HASH_SIZE 26
#define WORD_LENGTH 25
#define ANAGRAM_LENGTH 21
#define MAX_POSSIBLE_WORDS 2000
#define COMB3_ITERATIONS 743255415 /* = (1647 x 1646 x 1645) / (3 x 2) */
#define COMB4_ITERATIONS 305477975565 /* = (1647 x 1646 x 1645 x 1644) / (4 x 3 x 2) */
long int count = 0;
struct word {
unsigned short int len;
unsigned short int hash [HASH_SIZE];
char value[];
};
int getHash(const char * word, unsigned short int * const hash) {
int i;
for (i = 0; i < strlen(word); i += 1) {
char c = word[i];
if (c == ' ') continue; /* Skip space character */
int index = c - 97;
if (index < 0 || index > 25) return 0;
hash[index] = hash[index] != 0 ? hash[index] + 1 : 1;
}
return 1;
}
int isValidHash(const char * word, const unsigned short int * knownHash) {
unsigned short int hash[HASH_SIZE] = {0};
int canHash = getHash(word, hash);
int i = 0;
for (i = 0; i < HASH_SIZE; i += 1) {
if (canHash == 0 || hash[i] > knownHash[i]) {
return 0;
}
}
return 1;
}
int isKnownWord(struct word * words[], const char * newWord, int possibleWordsCount) {
int i = 0;
for (i = 0; i < possibleWordsCount; i += 1) {
if (strcmp(words[i]->value, newWord) == 0) {
return 1;
}
}
return 0;
}
int getPossibleWord(const char * filepath, unsigned short int * knownAnagramHash, struct word * words[]) {
FILE * inputFd;
char line[WORD_LENGTH];
int wordIndex = -1;
inputFd = fopen(filepath, "r");
if (inputFd == NULL) {
printf("opening file %s failed\n", filepath);
return 0;
}
while(fgets(line, WORD_LENGTH, inputFd) != NULL) {
/* Strip LF & CRLF */
line[strcspn(line, "\r\n")] = '\0';
if (*line == '\0') continue;
unsigned int len = strlen(line);
if (len == 1) continue; /* Skip one char lines */
int isMyHashValid = isValidHash(line, knownAnagramHash);
if (isMyHashValid == 0) continue;
int isMyWordKnown = isKnownWord(words, line, wordIndex + 1);
if (isMyWordKnown == 1) continue;
wordIndex +=1;
strcpy(words[wordIndex]->value, line);
words[wordIndex]->len = len;
getHash(line, words[wordIndex]->hash);
}
if (fclose(inputFd) == -1) {
printf("close input file %s failed\n", filepath);
return 0;
}
return wordIndex + 1;
}
void md5sum(unsigned char anagram[], int len, char md5[]) {
unsigned char md5sum[MD5_DIGEST_LENGTH];
MD5(anagram, len, md5sum);
int i;
for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(&md5[i*2], "%02x", (unsigned int)md5sum[i]);
}
}
int computeAnagram(struct word * const words[], const unsigned short int indices[], const int wordsCount, unsigned char anagram[]) {
int i, j, k = 0;
for (i = 1; i <= wordsCount; i += 1) {
for (j = 0; j < words[indices[i]]->len; j += 1) {
anagram[k + j] = words[indices[i]]->value[j];
}
k += j;
anagram[k++] = ' ';
}
anagram[--k] = '\0';
return k;
}
/* TODO: Check other algorithm for generating permutation. This one called
* heap recursive https://en.wikipedia.org/wiki/Heap%27s_algorithm */
void enumerateAllAnagrams(struct word * const words[], int n, unsigned short int c[], int size) {
if (n == 1) {
unsigned char anagram[ANAGRAM_LENGTH+1] = {0};
int anagramLen = computeAnagram(words, c, size, anagram);
char md5[33];
md5sum(anagram, anagramLen, md5);
if ( strcmp(md5, "e4820b45d2277f3844eac66c903e84be") != 0 &&
strcmp(md5, "23170acc097c24edb98fc5488ab033fe") != 0 &&
strcmp(md5, "665e5bcb0c20062fe8abaaf4628bb154") != 0 ) return;
printf("solution: %s\n", anagram);
} else {
int m;
for (m = 1; m < n; m += 1) {
enumerateAllAnagrams(words, n-1, c, size);
if (n % 2 == 0) {
int tmp = c[m];
c[m] = c[n];
c[n] = tmp;
} else {
int tmp = c[1];
c[1] = c[n];
c[n] = tmp;
}
}
enumerateAllAnagrams(words, n-1, c, size);
}
}
void processComb(const unsigned short int c[], int size, struct word * const words[], unsigned short int const * const knownHash) {
int m = 0;
unsigned short int hash[HASH_SIZE] = {0};
unsigned short int len = 0;
/* TODO: Check possibility to use SIMD */
for(m = 1; m <= size; m += 1) {
hash[0] += words[c[m]]->hash[0]; /* letter: a */
hash[8] += words[c[m]]->hash[8]; /* letter: i */
hash[11] += words[c[m]]->hash[11]; /* letter: l */
hash[13] += words[c[m]]->hash[13]; /* letter: n */
hash[14] += words[c[m]]->hash[14]; /* letter: o */
hash[15] += words[c[m]]->hash[15]; /* letter: p */
hash[17] += words[c[m]]->hash[17]; /* letter: r */
hash[18] += words[c[m]]->hash[18]; /* letter: s */
hash[19] += words[c[m]]->hash[19]; /* letter: t */
hash[20] += words[c[m]]->hash[20]; /* letter: u */
hash[22] += words[c[m]]->hash[22]; /* letter: w */
hash[24] += words[c[m]]->hash[24]; /* letter: y */
len += words[c[m]]->len;
}
if (memcmp(hash, knownHash, HASH_SIZE) != 0) return;
if (len + 3 > ANAGRAM_LENGTH) return;
unsigned short int newC[size+1];
for(m = 1; m <= size; m += 1) {
newC[m] = c[m];
}
enumerateAllAnagrams(words, size, newC, size);
}
/* Revolving door algorithm from D. Knuth Volume 4A */
void combRevolving(int t, int n, unsigned short int c[], struct word * const words[],
unsigned short int const * const knownHash) {
int j;
for (j = 1; j <= t; j += 1) {
c[j] = j - 1;
}
c[j] = n;
while(1) {
count += 1;
processComb(c, t, words, knownHash);
if (t % 2 != 0) {
if (c[1] + 1 < c[2]) {
c[1] += 1;
continue;
} else {
j = 2;
goto R4;
}
} else {
if (c[1] > 0) {
c[1] -= 1;
continue;
} else {
j = 2;
goto R5;
}
}
R4: if (c[j] >= j) {
c[j] = c[j-1];
c[j-1] = j - 2;
continue;
} else {
j += 1;
}
R5: if (c[j] + 1 < c[j+1]) {
c[j-1] = c[j];
c[j] = c[j] + 1;
continue;
} else {
j += 1;
if (j <= t) {
goto R4;
} else {
return;
}
}
}
}
void allocateWords(struct word * words[]) {
int i = 0, j = 0;
for (i = 0; i < MAX_POSSIBLE_WORDS; i += 1) {
words[i] = malloc(WORD_LENGTH * sizeof(char) + (HASH_SIZE + 1) * sizeof(int));
strcpy(words[i]->value, "\0");
words[i]->len = 0;
for (j = 0; j < HASH_SIZE; j += 1) {
words[i]->hash[j] = 0;
}
}
}
void releaseWords(struct word * words[]) {
int i =0;
for (i = 0; i < MAX_POSSIBLE_WORDS; i += 1) {
free(words[i]);
}
}
int main(int argc, char *argv[]) {
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
printf("Usage: %s worldlist\n", argv[0]);
return 1;
}
const char * knownAnagram = "poultry outwits ants";
unsigned short int knownAnagramHash[HASH_SIZE] = {0};
getHash(knownAnagram, knownAnagramHash);
struct word * words[MAX_POSSIBLE_WORDS];
allocateWords(words);
const char * filepath = argv[1];
int possibleWordsCount = getPossibleWord(filepath, knownAnagramHash, words);
if (possibleWordsCount == 0) {
printf("No possible words found!\n");
return 1;
}
long int comb3Iterations = possibleWordsCount *
(possibleWordsCount - 1) * (possibleWordsCount - 2) / 6;
long int comb4Iterations = possibleWordsCount *
(possibleWordsCount - 1) * (possibleWordsCount - 2) * (possibleWordsCount - 3) / 24;
printf("Found %i possible words\n", possibleWordsCount);
printf("It will take roughly %lu iteration to do comb3\n", comb3Iterations);
printf("It will take roughly %lu iteration to do comb4\n", comb4Iterations);
int n = possibleWordsCount;
int t;
t = 3;
unsigned short int c[t+2];
combRevolving(t, n, c, words, knownAnagramHash);
t = 4;
unsigned short int d[t+2];
combRevolving(t, n, d, words, knownAnagramHash);
releaseWords(words);
return 0;
}