-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrackit.c
159 lines (132 loc) · 4.39 KB
/
crackit.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
// function referenced by pthread_create to execute on background thread
void *eachCountTo(volatile unsigned long long *execOn) {
volatile unsigned long long counter = *execOn;
while (counter) {
counter--;
}
return NULL;
}
/// Formats an unsigned long long to a char * using commas, caller is responsible for freeing the return value
char *formatllu(unsigned long long n) {
char largestllu[22];
int fixModLen = sprintf(largestllu, "%llu", n);
int commas = fixModLen/3;
if (commas) {
commas--;
}
char *ret = malloc(fixModLen+commas+1);
int offset = fixModLen%3;
char *lchr = largestllu;
char *rchr = ret;
for (int i = 0; i < fixModLen; i++) {
if ((i%3 == offset) && i) {
*rchr = ',';
rchr++;
}
*rchr = *lchr;
rchr++;
lchr++;
}
*rchr = '\0';
return ret;
}
/// Formats a double to a char * using commas, caller is responsible for freeing the return value
char *formatDouble(double n) {
double lv = floor(n);
char largestBuff[36];
char *front = formatllu((unsigned long long)lv);
unsigned int realLen = sprintf(largestBuff, "%s %f", front, n-lv)-2;
free(front);
char *ret = malloc(realLen+1);
char *b = largestBuff;
char *r = ret;
for (int i = 0; i < realLen; i++) {
if (*b == ' ') {
b += 2;
}
*r = *b;
r++;
b++;
}
*r = '\0';
return ret;
}
// Thanks https://stackoverflow.com/a/7918955
double absoluteCurrentTime() {
struct timeval t;
gettimeofday(&t, NULL);
return (1.0e-6 * t.tv_usec) + t.tv_sec;
}
int main(int argc, const char *argv[]) {
const char *argOne = argv[1];
const char *argTwo = argv[2];
unsigned long long possibilities = 0;
long numberOfThreads = 0;
if (argOne) {
long long check = atoll(argOne);
if (check < 0) {
puts("possibilities can not be a negative number");
return 1;
}
possibilities = check;
}
if (argTwo) {
long check = atol(argTwo);
if (check < 0) {
puts("threads can not be a negative number");
return 1;
}
numberOfThreads = check;
}
// if the numberOfThreads arg was not provided, set it to the number of device cores
if (!numberOfThreads) {
// Thanks https://stackoverflow.com/a/4586471
numberOfThreads = sysconf(_SC_NPROCESSORS_ONLN);
}
if ((argc > 3) || !possibilities) {
printf("Usage: %s <possibilities> [threads]\n"
" 'threads' defaults to the amount of machine cores\n", argv[0]);
return 1;
}
// calculate how much to count on each thread
unsigned long long execPer = possibilities/numberOfThreads;
// store the current time to calculate later
double startTime = absoluteCurrentTime();
// create an array of threadIDs with the amount of threads to use
pthread_t threads[numberOfThreads];
// kick off the amount of threads specified
for (unsigned int thread = 0; thread < numberOfThreads; thread++) {
pthread_create(&threads[thread], NULL, (void *)eachCountTo, &execPer);
}
// fix any remainders caused by the original mod operation
// executed on main thread while background threads are potentially still running
unsigned long long fixMod = execPer*numberOfThreads;
while (fixMod < possibilities) {
fixMod++;
}
// go through each of the threads and wait until they are finished
for (unsigned int thread = 0; thread < numberOfThreads; thread++) {
pthread_join(threads[thread], NULL);
}
// store the time after all counting operations have finished
double endTime = absoluteCurrentTime();
// this timing mechanism has been tested extensively using radare2 (?t !crackit <number>) and is reliable
double totalTime = endTime-startTime;
printf("Count to: %s\n"
"Threads: %ld\n"
"Millisecs: %f\n"
"Seconds: %f\n"
"Per sec: %s\n",
formatllu(fixMod),
numberOfThreads,
totalTime*1000,
totalTime,
formatDouble(fixMod/totalTime));
return 0;
}