forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashlib.cpp
284 lines (214 loc) · 7.18 KB
/
hashlib.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "hashlib.h"
std::vector<std::string> HashLib::hashes( { "MD4", "MD5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512", "MySQL4.1+", "RIPEMD-160", "Whirlpool", "LM", "NTLM" } );
HashLib * HashLib::getHasher( std::string hashName ) {
removeChars( hashName, "-+., " );
strToLower( hashName );
if ( hashName == "md4" ) {
return new HashMD4();
} else if ( hashName == "md5" ) {
return new HashMD5();
} else if ( (hashName == "sha1") || (hashName == "sha") ) {
return new HashSHA1();
} else if ( hashName == "sha224" ) {
return new HashSHA224();
} else if ( hashName == "sha256" ) {
return new HashSHA256();
} else if ( hashName == "sha384" ) {
return new HashSHA384();
} else if ( hashName == "sha512" ) {
return new HashSHA512();
} else if ( (hashName == "mysql41") || (hashName == "mysql") ) {
return new HashMySQL41();
} else if ( (hashName == "ripemd160") || (hashName == "ripemd") ) {
return new HashRIPEMD160();
} else if ( hashName == "whirlpool" ) {
return new HashWhirlpool();
} else if ( hashName == "lm" ) {
return new HashLM();
} else if ( hashName == "ntlm" ) {
return new HashNTLM();
} else {
throw std::invalid_argument( "The hash type \"" + hashName + "\" is unknown!" );
}
}
const std::vector<std::string>& HashLib::getHashes() {
return hashes;
}
std::string HashLib::getHashesStr( const std::string & delim ) {
return join( hashes, delim );
}
HashLib::Hash HashLib::hash( const char * stringToHash ) {
return hash( reinterpret_cast<const byte *>(stringToHash), strlen( stringToHash ) );
}
HashLib::Hash HashLib::hash( const std::string & stringToHash ) {
return hash( reinterpret_cast<const byte *>(stringToHash.c_str()), stringToHash.length() );
}
HashLib::Hash::Hash() {}
HashLib::Hash::Hash( byte * bytes, size_type size ) : bytes( bytes, bytes + size ) {}
HashLib::Hash::Hash( const Hash & lhs ) : bytes( lhs.bytes ) {}
byte HashLib::Hash::getNthByte( HashLib::size_type index ) const {
if ( index < bytes.size() )
return bytes[index];
else
return 0;
}
HashLib::size_type HashLib::Hash::getLength() const {
return bytes.size();
}
std::string HashLib::Hash::toString() const {
std::ostringstream sstr;
sstr << std::hex << std::uppercase << std::setfill( '0' );
for ( const byte & byteToWrite : bytes ) {
sstr << std::setw( 2 ) << (int)byteToWrite;
}
return sstr.str();
}
void HashLib::Hash::fromString( std::string str ) {
strToLower( str );
keepChars( str, "0123456789abcdef" );
const size_t newSize = str.size() / 2;
bytes.resize( newSize );
for ( size_t i = 0; i < newSize; i++ ) {
bytes[i] = (hexCharToByte( str[2 * i] ) << 4) | hexCharToByte( str[2 * i + 1] );
}
}
bool HashLib::Hash::partialMatch( const Hash & lhs ) const {
for ( size_t i = 0; i < bytes.size(); i++ ) {
if ( bytes[i] != lhs.bytes[i] )
return false;
}
return true;
}
HashLib::Hash & HashLib::Hash::operator=( const Hash & lhs ) {
bytes = lhs.bytes;
return *this;
}
bool HashLib::Hash::operator==( const Hash & lhs ) const {
return bytes == lhs.bytes;
}
bool HashLib::Hash::operator!=( const Hash & lhs ) const {
return !(*this == lhs);
}
byte HashLib::Hash::operator[]( size_type index ) const {
return getNthByte( index );
}
std::ostream & operator<<( std::ostream & rhs, const HashLib::Hash & lhs ) {
rhs << lhs.toString();
return rhs;
}
// Hashing algorithms:
// MD4
HashLib::Hash HashMD4::hash( const byte * stringToHash, size_t strSize ) {
MD4( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashMD4::getLength() {
return length;
}
// MD5
HashLib::Hash HashMD5::hash( const byte * stringToHash, size_t strSize ) {
MD5( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashMD5::getLength() {
return length;
}
// SHA-1
HashLib::Hash HashSHA1::hash( const byte * stringToHash, size_t strSize ) {
SHA1( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashSHA1::getLength() {
return length;
}
// SHA-224
HashLib::Hash HashSHA224::hash( const byte * stringToHash, size_t strSize ) {
SHA224( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashSHA224::getLength() {
return length;
}
// SHA-256
HashLib::Hash HashSHA256::hash( const byte * stringToHash, size_t strSize ) {
SHA256( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashSHA256::getLength() {
return length;
}
// SHA-384
HashLib::Hash HashSHA384::hash( const byte * stringToHash, size_t strSize ) {
SHA384( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashSHA384::getLength() {
return length;
}
// SHA-512
HashLib::Hash HashSHA512::hash( const byte * stringToHash, size_t strSize ) {
SHA512( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashSHA512::getLength() {
return length;
}
// MySQL4.1+
HashLib::Hash HashMySQL41::hash( const byte * stringToHash, size_t strSize ) {
SHA1( stringToHash, strSize, hashStorage );
SHA1( hashStorage, length, hashStorage2 );
return HashLib::Hash( hashStorage2, length );
}
HashLib::size_type HashMySQL41::getLength() {
return length;
}
// RIPEMD-160
HashLib::Hash HashRIPEMD160::hash( const byte * stringToHash, size_t strSize ) {
RIPEMD160( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashRIPEMD160::getLength() {
return length;
}
// Whirlpool
HashLib::Hash HashWhirlpool::hash( const byte * stringToHash, size_t strSize ) {
WHIRLPOOL( stringToHash, strSize, hashStorage );
return HashLib::Hash( hashStorage, length );
}
HashLib::size_type HashWhirlpool::getLength() {
return length;
}
// LM
HashLib::Hash HashLM::hash( const byte * stringToHash, size_t strSize ) {
std::array<byte, 14> paddedString( { 0 } );
for ( size_t i = 0; i < std::min<size_t>( strSize, 14 ); i++ )
paddedString[i] = toupper( stringToHash[i] );
DESencrypt( paddedString.data(), hashStorage );
DESencrypt( paddedString.data() + 7, hashStorage + 8 );
return Hash( hashStorage, length );
}
HashLib::size_type HashLM::getLength() {
return length;
}
void HashLM::DESencrypt( const byte * data, byte * storage ) {
std::array<byte, 8> key( { 0 } );
DES_key_schedule keysched;
key[0] = (data[0] & 254);
key[1] = (data[0] << 7) | (data[1] >> 1);
key[2] = (data[1] << 6) | (data[2] >> 2);
key[3] = (data[2] << 5) | (data[3] >> 3);
key[4] = (data[3] << 4) | (data[4] >> 4);
key[5] = (data[4] << 3) | (data[5] >> 5);
key[6] = (data[5] << 2) | (data[6] >> 6);
key[7] = (data[6] << 1);
DES_set_key( reinterpret_cast<const_DES_cblock *>(key.data()), &keysched );
DES_ecb_encrypt( reinterpret_cast<const_DES_cblock *>(message), reinterpret_cast<const_DES_cblock *>(storage), &keysched, DES_ENCRYPT );
}
// NTLM
HashLib::Hash HashNTLM::hash( const byte * stringToHash, size_t strSize ) {
// Using smart pointer to enable RAII
std::unique_ptr<byte[]> paddedString( new byte[strSize * 2]() );
for ( size_t i = 0; i < strSize; i++ )
paddedString[2 * i] = stringToHash[i];
return HashMD4::hash( paddedString.get(), strSize * 2 );
}