-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1504_md5_secret_key.cpp
197 lines (167 loc) Β· 3.9 KB
/
aoc1504_md5_secret_key.cpp
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
#include "iostream"
using namespace std;
// Example
// string data = "jdfgsdhfsdfsd 156445dsfsd7fg/*/+bfjsdgf%$^";
// string value = GetMD5String(data);
// Output
// 67046f2ecf6d4a8aee9dedcffc35b91b
static string GetMD5String(string);
int main(int c, char **v)
{
if (c != 2)
return 0;
cout << v[1] << endl;
string key = v[1];
int found5 = 0;
int n = 0;
while (42)
{
string new_key = key + to_string(n);
string s = GetMD5String( new_key );
int i = 0;
while (i < 6)
{
if (s[i] != '0')
break;
i++;
if (i == 5 && found5 == 0)
{
cout << "part 1 : " << n << endl;
found5++;
}
if (i == 6)
{
cout << "part 2 : " << n << endl;
break ;
}
}
if (i == 6)
break ;
n++;
}
}
// MD5 hash function ::
//
// programmingalgorithms.com/algorithm/md5/
//
/*****Please include following header files*****/
// string cmath
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
//#include "string"
#include "cmath"
typedef union uwb {
unsigned w;
unsigned char b[4];
} MD5union;
typedef unsigned DigestArray[4];
static unsigned func0(unsigned abcd[]){
return (abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);
}
static unsigned func1(unsigned abcd[]){
return (abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);
}
static unsigned func2(unsigned abcd[]){
return abcd[1] ^ abcd[2] ^ abcd[3];
}
static unsigned func3(unsigned abcd[]){
return abcd[2] ^ (abcd[1] | ~abcd[3]);
}
typedef unsigned(*DgstFctn)(unsigned a[]);
static unsigned *calctable(unsigned *k)
{
double s, pwr;
int i;
pwr = pow(2.0, 32);
for (i = 0; i<64; i++) {
s = fabs(sin(1.0 + i));
k[i] = (unsigned)(s * pwr);
}
return k;
}
static unsigned rol(unsigned r, short N)
{
unsigned mask1 = (1 << N) - 1;
return ((r >> (32 - N)) & mask1) | ((r << N) & ~mask1);
}
static unsigned* MD5Hash(string msg)
{
int mlen = msg.length();
static DigestArray h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3 };
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7, 12, 17, 22 };
static short rot1[] = { 5, 9, 14, 20 };
static short rot2[] = { 4, 11, 16, 23 };
static short rot3[] = { 6, 10, 15, 21 };
static short *rots[] = { rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned *k;
static DigestArray h;
DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
short *rotn;
union {
unsigned w[16];
char b[64];
}mm;
int os = 0;
int grp, grps, q, p;
unsigned char *msg2;
if (k == NULL) k = calctable(kspace);
for (q = 0; q<4; q++) h[q] = h0[q];
{
grps = 1 + (mlen + 8) / 64;
msg2 = (unsigned char*)malloc(64 * grps);
memcpy(msg2, msg.c_str(), mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64 * grps){ msg2[q] = 0; q++; }
{
MD5union u;
u.w = 8 * mlen;
q -= 8;
memcpy(msg2 + q, &u.w, 4);
}
}
for (grp = 0; grp<grps; grp++)
{
memcpy(mm.b, msg2 + os, 64);
for (q = 0; q<4; q++) abcd[q] = h[q];
for (p = 0; p<4; p++) {
fctn = ff[p];
rotn = rots[p];
m = M[p]; o = O[p];
for (q = 0; q<16; q++) {
g = (m*q + o) % 16;
f = abcd[1] + rol(abcd[0] + fctn(abcd) + k[q + 16 * p] + mm.w[g], rotn[q % 4]);
abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];
abcd[1] = f;
}
}
for (p = 0; p<4; p++)
h[p] += abcd[p];
os += 64;
}
return h;
}
static string GetMD5String(string msg) {
string str;
int j;//, k;
unsigned *d = MD5Hash(msg);
MD5union u;
for (j = 0; j<4; j++){
u.w = d[j];
char s[9];
sprintf(s, "%02x%02x%02x%02x", u.b[0], u.b[1], u.b[2], u.b[3]);
str += s;
}
return str;
}