-
Notifications
You must be signed in to change notification settings - Fork 1
/
md5.cpp
217 lines (168 loc) · 4.38 KB
/
md5.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "md5.hpp"
#include <sstream>
#include <iomanip>
#include <cstring>
bool determine_endianness() {
int32_t val = 1;
return *reinterpret_cast<char*>(&val);
}
const bool LITTLE_ENDIAN = determine_endianness();
uint32_t F(uint32_t X, uint32_t Y, uint32_t Z) {
return (X & Y) | ((~X) & Z);
}
uint32_t G(uint32_t X, uint32_t Y, uint32_t Z) {
return (X & Z) | (Y & (~Z));
}
uint32_t H(uint32_t X, uint32_t Y, uint32_t Z) {
return X ^ Y ^ Z;
}
uint32_t I(uint32_t X, uint32_t Y, uint32_t Z) {
return Y ^ (X | (~Z));
}
template<typename T>
inline void rotate_arguments_right(T& A, T& B, T& C, T& D) {
std::swap(A, D);
std::swap(D, C);
std::swap(C, B);
}
const uint32_t MD5::shift_table[4][4] = {
{7, 12, 17, 22},
{5, 9, 14, 20},
{4, 11, 16, 23},
{6, 10, 15, 21}
};
const MD5::Round_function MD5::round_functions[4] = { F,G,H,I };
MD5::MD5(std::string _input) {
std::vector<char> input;
for (char x : _input) {
input.push_back(x);
}
pad_input(input);
compute_hash(input.data(), input.size());
}
MD5::MD5(std::ifstream& file) {
if (!file.is_open()) {
throw std::runtime_error("Invalid file.");
}
file.seekg(0, std::ios::end);
std::size_t input_size = file.tellg();
std::vector<char>input(input_size, 0);
file.seekg(0, std::ios::beg);
file.read(input.data(), input_size);
pad_input(input);
compute_hash(input.data(), input.size());
}
void MD5::pad_input(std::vector<char>& input) {
uint64_t original_size = static_cast<uint64_t>(input.size());
//Inserts padding
input.push_back(0x80);
do {
input.push_back(0);
} while (input.size() % 56);
do {
input.push_back(0);
} while (input.size() % 64);
uint64_t size = original_size * 8;
void* size_section = reinterpret_cast<uint64_t*>(input.data() + input.size()) - 1;
memcpy(size_section, &size, sizeof(uint64_t));
}
void MD5::compute_hash(char* message, size_t size) {
if (!LITTLE_ENDIAN)
encode(message, size);
for (size_t i = 0; i < size; i+= 64, message += 64) {
process_block(message);
}
if (LITTLE_ENDIAN) {
change_endianness((char*)&A);
change_endianness((char*)&B);
change_endianness((char*)&C);
change_endianness((char*)&D);
}
}
void MD5::process_block(char* block) {
uint32_t* block_as_word = reinterpret_cast<uint32_t*>(block);
auto OLD_A = A;
auto OLD_B = B;
auto OLD_C = C;
auto OLD_D = D;
for (int i = 0; i < 64; ++i) {
int round = i / 16;
uint32_t s = shift_table[round][i % 4];
int k;
switch (round) {
case 0:
k = i;
break;
case 1:
k = (1 + 5 * (i - round * 16)) % 16;
break;
case 2:
k = (5 + 3 * (i - round * 16)) % 16;
break;
case 3:
k = (7 * (i - round * 16)) % 16;
break;
}
A = A + round_functions[round](B, C, D) + block_as_word[k] + sin_table()[i];
A = (A << s) | (A >> (32 - s));
A += B;
rotate_arguments_right(A, B, C, D);
}
A += OLD_A;
B += OLD_B;
C += OLD_C;
D += OLD_D;
}
void MD5::encode(char* message, size_t size) {
for (size_t i = 0; i != size; i += 4) {
change_endianness(message + 1);
}
}
inline void MD5::change_endianness(char* val) {
//Bitwise "trick" to swap values
val[0] ^= (val[3] ^= (val[0] ^= val[3]));
val[1] ^= (val[2] ^= (val[1] ^= val[2]));
}
const uint32_t* MD5::create_table() {
static uint32_t values[64];
for (int i = 0; i < 64; ++i) {
values[i] = static_cast<uint32_t>(std::abs(std::sin(i + 1) * 4294967296LL));
}
return values;
}
const uint32_t* MD5::sin_table() {
static const uint32_t* table = create_table();
return table;
}
std::string MD5::get_string_hash() {
std::stringstream ss;
ss << std::hex << std::setw(8) << std::setfill('0') << A << B << C << D;
std::string ret_val;
ss >> ret_val;
return ret_val;
}
MD5::MD5(const MD5& other) {
*this = other;
}
MD5& MD5::operator=(const MD5& other) {
this->A = other.A;
this->B = other.B;
this->C = other.C;
this->D = other.D;
return *this;
}
MD5& MD5::operator=(MD5&& other) {
if (this != &other) {
this->A = other.A;
this->B = other.B;
this->C = other.C;
this->D = other.D;
}
return *this;
}
bool MD5::operator==(const MD5& other) {
return this->A == other.A && this->B == other.B && this->C == other.C && this->D == other.D;
}
bool MD5::operator!=(const MD5& other) {
return !(*this == other);
}