1 /* Tries to find data with a given MD5 (up to N bits). */
2 #include "ccan/antithread/antithread.h"
3 #include "ccan/string/string.h"
4 #include "ccan/talloc/talloc.h"
5 #include "md5_finder.h"
7 #include <sys/select.h>
11 static void usage(void)
13 errx(1, "Usage: md5calc <hexstring> <numcpus>");
16 static void parse_hexstring(const char *string, struct md5_search *md5s)
20 if (strstarts(string, "0x") || strstarts(string, "0X"))
23 for (i = 0; i < MD5_HASH_WORDS; i++) {
27 ret = sscanf(string, "%02x%02x%02x%02x",
28 &n[0], &n[1], &n[2], &n[3]);
33 for (j = 0; j < ret; j++) {
34 md5s->mask[MD5_HASH_WORDS-i-1] |= (0xFF << (8*j));
35 md5s->md5[MD5_HASH_WORDS-i-1] |= (n[j] << (8*j));
43 static void init_pattern(u8 *pattern, unsigned int num_bytes, u64 total)
47 for (i = 0; i < num_bytes; i++) {
48 pattern[i] = 'A' + (total % 26);
53 #define PATTERN_BYTES 32
55 int main(int argc, char *argv[])
58 struct md5_search md5s;
59 unsigned int i, maxfd, numathreads = argc == 3 ? atoi(argv[2]) : 0;
62 char *cmdline[] = { "./md5_worker", NULL };
63 struct athread *at[numathreads];
68 memset(&md5s, 0, sizeof(md5s));
69 parse_hexstring(argv[1], &md5s);
71 md5s.num_tries = 1024*1024;
72 md5s.num_bytes = PATTERN_BYTES;
74 /* *2 to allow for allocation inefficiency. */
75 atp = at_pool((sizeof(md5s) + PATTERN_BYTES) * (numathreads + 1) * 2);
77 err(1, "Can't create pool");
79 /* Free pool on exit. */
80 // talloc_steal(talloc_autofree_context(), atp);
84 for (i = 0; i < numathreads; i++) {
85 at[i] = at_spawn(atp, NULL, cmdline);
87 err(1, "Can't spawn child");
88 FD_SET(at_fd(at[i]), &fds);
89 if (at_fd(at[i]) > maxfd)
94 struct md5_search *m, *res;
98 m = talloc(at_pool_ctx(atp), struct md5_search);
101 m->pattern = talloc_array(m, u8, m->num_bytes);
102 init_pattern(m->pattern, m->num_bytes, total);
104 select(maxfd+1, &in, NULL, NULL, NULL);
105 for (i = 0; i < numathreads; i++)
106 if (FD_ISSET(at_fd(at[i]), &in))
108 if (i == numathreads)
109 errx(1, "Select returned, but noone ready?");
111 res = at_read(at[i]);
113 warn("Thread died?");
114 FD_CLR(at_fd(at[i]), &fds);
117 if (res != INITIAL_POINTER) {
119 printf("Success! '%.*s'\n",
120 res->num_bytes, (char *)res->pattern);
127 total += m->num_tries;