This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess.c
100 lines (83 loc) · 2.08 KB
/
guess.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
#include "assert.h"
#include "stdbool.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
extern const char* password;
bool checkPassword(char* input) {
const char* p = password;
const char* i = input;
while (*i && *p) {
if (*i != *p) {
return false;
}
i++;
p++;
}
return *i == *p;
}
bool checkPasswordConstant(char* input) {
const char* p = password;
const char* i = input;
int same = 1;
while (*i && *p) {
same &= (*i == *p);
i++;
p++;
}
return same & (*i == *p);
}
uint64_t timeInput(char* input, int iterations) {
uint64_t before = __rdtsc();
for (int i = 0; i < iterations; i++) {
checkPassword(input);
}
uint64_t after = __rdtsc();
return after - before;
}
int iterations = 100;
const char* alphabet = "0123456789abcdefghijklkmnopqrstuvwxyz-";
// Given a buffer with some characters in it, set the letter at
// index to all the letters in the alphabet, and collect the runtimes.
void testLetters(char* buffer, size_t index, uint64_t* times) {
size_t alphalen = strlen(alphabet);
assert(buffer[index + 1] == '\0');
for (size_t alphaIndex = 0; alphaIndex < alphalen; alphaIndex++) {
buffer[index] = alphabet[alphaIndex];
times[alphaIndex] = timeInput(buffer, iterations);
}
}
bool guessLetter(char* buffer, size_t index) {
size_t alphalen = strlen(alphabet);
uint64_t times[alphalen];
testLetters(buffer, index, times);
uint64_t max = 0;
int maxIndex = -1;
for (int i = 0; i < alphalen; i++) {
if (times[i] > max) {
maxIndex = i;
max = times[i];
}
}
buffer[index] = alphabet[maxIndex];
printf("Guessing letter %zu is %c making current buffer %s\n", index,
alphabet[maxIndex], buffer);
return checkPassword(buffer);
}
void guessPassword() {
char buffer[100] = {0};
for (int i = 0; i < 99; i++) {
if (guessLetter(buffer, i)) {
printf("Found password %s\n", buffer);
return;
}
}
printf("Didn't find password\n");
}
int main(int argc, char** argv) {
if (argc > 1) {
iterations = atoi(argv[1]);
}
guessPassword();
}