-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
306 lines (229 loc) · 7.53 KB
/
main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <iostream>
#ifdef _WIN32
#include <time.h>
#include <windows.h>
#else
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#include "amx/amx.h"
#include "plugincommon.h"
#include "settings.h"
#include "md5.h"
#include "sha1.h"
#include "sha2.h"
#include "AmxUtils.h"
#include "Base64.h"
#include "whirlpool.h"
#include "bcrypt.h"
#ifdef _WIN32
#pragma comment(lib, "advapi32.lib")
#endif
typedef void(*logprintf_t)(char* format, ...);
logprintf_t logprintf;
extern void *pAMXFunctions;
cell AMX_NATIVE_CALL hashlib_md5(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, md5(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_sha1(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, sha1(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_sha224(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, sha224(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_sha256(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, sha256(str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_sha384(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, sha384(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_sha512(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, sha512(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_whirlpool(AMX* amx, cell* params)
{
std::string str = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string salt = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
cell *addr = NULL;
amx_GetAddr(amx, params[3], &addr);
amx_SetString(addr, whirlpool(salt + str).c_str(), 0, 0, params[4]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_bcrypt(AMX* amx, cell* params)
{
std::string input = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
int cost_factor = params[4];
if (cost_factor < 4 || cost_factor > 31)
{
logprintf("[ERROR] hashlib: The cost factor for hashlib_bcrypt() can only be between 4 and 31!");
return 0;
}
cell *addr = NULL;
char hash[BCRYPT_HASHSIZE] = { 0 };
char salt[BCRYPT_HASHSIZE] = { 0 };
bcrypt_gensalt(cost_factor, salt);
bcrypt_hashpw(input.c_str(), salt, hash);
amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, hash, 0, 0, params[3]);
return 1;
}
cell AMX_NATIVE_CALL hashlib_bcrypt_compare(AMX* amx, cell* params)
{
std::string provided = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
std::string expected = AmxUtils::amx_GetStdString(amx, ¶ms[2]);
return (bcrypt_checkpw(provided.c_str(), expected.c_str()) == 0);
}
cell AMX_NATIVE_CALL hashlib_generate_salt(AMX* amx, cell* params)
{
int len = params[3];
if (len < 512) len = 512;
if (len > 4096) len = 4096;
#ifdef _WIN32
HCRYPTPROV hProvider = 0;
if (!::CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
logprintf("[ERROR] hashlib: CryptAcquireContextW failed!");
return 0;
}
BYTE *rbytes = new BYTE[len];
if (!::CryptGenRandom(hProvider, len, rbytes))
{
logprintf("[ERROR] hashlib: CryptGenRandom failed! Could not generate random bytes!");
::CryptReleaseContext(hProvider, 0);
delete[] rbytes;
return 0;
}
std::string out(reinterpret_cast<char const*>(rbytes), len);
cell *addr = NULL;
amx_GetAddr(amx, params[1], &addr);
amx_SetString(addr, sha224(out).c_str(), 0, 0, params[2]);
if (!::CryptReleaseContext(hProvider, 0))
{
logprintf("[ERROR] hashlib: CryptReleaseContext failed!");
delete[] rbytes;
return 0;
}
return 1;
#else
char *rbytes = new char[len];
int f = open("/dev/random", O_RDONLY);
if (!f)
{
logprintf("[ERROR] hashlib: Could not open /dev/random!");
delete[] rbytes;
return 0;
}
read(f, rbytes, sizeof(rbytes));
close(f);
std::string out = std::string(rbytes);
cell *addr = NULL;
amx_GetAddr(amx, params[1], &addr);
amx_SetString(addr, sha224(out).c_str(), 0, 0, params[2]);
delete[] rbytes;
return 1;
#endif
}
cell AMX_NATIVE_CALL hashlib_base64_encode(AMX* amx, cell* params)
{
cell *addr = NULL;
std::string text = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
if (text.length() >= 1)
{
std::string out = Base64::encode(text);
amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, out.c_str(), 0, 0, params[3]);
}
return 1;
}
cell AMX_NATIVE_CALL hashlib_base64_decode(AMX* amx, cell* params)
{
cell *addr = NULL;
std::string text = AmxUtils::amx_GetStdString(amx, ¶ms[1]);
if (text.length() >= 1)
{
std::string out = Base64::decode(text);
amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, out.c_str(), 0, 0, params[3]);
}
return 1;
}
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
{
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
logprintf(" --------------------------------------------------------");
logprintf(" hashlib version %s successfully loaded!", PLUGIN_VERSION);
logprintf(" Developer: Manuel Schnitzer");
logprintf(" Website: https://github.com/mschnitzer/hashlib");
logprintf(" --------------------------------------------------------");
return true;
}
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
logprintf(" * hashlib was unloaded.");
}
extern "C" const AMX_NATIVE_INFO PluginNatives[] =
{
{ "hashlib_md5", hashlib_md5 },
{ "hashlib_sha1", hashlib_sha1 },
{ "hashlib_sha224", hashlib_sha224 },
{ "hashlib_sha256", hashlib_sha256 },
{ "hashlib_sha384", hashlib_sha384 },
{ "hashlib_sha512", hashlib_sha512 },
{ "hashlib_whirlpool", hashlib_whirlpool },
{ "hashlib_bcrypt", hashlib_bcrypt },
{ "hashlib_bcrypt_compare", hashlib_bcrypt_compare },
{ "hashlib_generate_salt", hashlib_generate_salt },
{ "hashlib_base64_encode", hashlib_base64_encode },
{ "hashlib_base64_decode", hashlib_base64_decode },
{ 0, 0 }
};
PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
{
return amx_Register(amx, PluginNatives, -1);
}
PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
{
return AMX_ERR_NONE;
}