-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcrypt_node.cc
More file actions
286 lines (241 loc) · 10 KB
/
Copy pathbcrypt_node.cc
File metadata and controls
286 lines (241 loc) · 10 KB
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
#include <napi.h>
#include <string>
#include <cstring>
#include <vector>
#include <stdlib.h> // atoi
#include "node_blf.h"
#define NODE_LESS_THAN (!(NODE_VERSION_AT_LEAST(0, 5, 4)))
namespace {
bool ValidateSalt(const char* salt) {
if (!salt || *salt != '$') {
return false;
}
// discard $
salt++;
if (*salt > BCRYPT_VERSION) {
return false;
}
if (salt[1] != '$') {
switch (salt[1]) {
case 'a':
case 'b':
salt++;
break;
default:
return false;
}
}
// discard version + $
salt += 2;
if (salt[2] != '$') {
return false;
}
int n = atoi(salt);
if (n > 31 || n < 0) {
return false;
}
if (((uint8_t)1 << (uint8_t)n) < BCRYPT_MINROUNDS) {
return false;
}
salt += 3;
if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) {
return false;
}
return true;
}
inline char ToCharVersion(const std::string& str) {
return str[0];
}
/* SALT GENERATION */
class SaltAsyncWorker : public Napi::AsyncWorker {
public:
SaltAsyncWorker(const Napi::Function& callback, const std::string& seed, ssize_t rounds, char minor_ver)
: Napi::AsyncWorker(callback, "bcrypt:SaltAsyncWorker"), seed(seed), rounds(rounds), minor_ver(minor_ver) {
}
~SaltAsyncWorker() {}
void Execute() {
bcrypt_gensalt(minor_ver, rounds, (u_int8_t *)&seed[0], salt);
}
void OnOK() {
Napi::HandleScope scope(Env());
Callback().Call({Env().Undefined(), Napi::String::New(Env(), salt)});
}
private:
std::string seed;
ssize_t rounds;
char minor_ver;
char salt[_SALT_LEN];
};
Napi::Value GenerateSalt(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 4) {
throw Napi::TypeError::New(env, "4 arguments expected");
}
if (!info[0].IsString()) {
throw Napi::TypeError::New(env, "First argument must be a string");
}
if (!info[2].IsBuffer() || (info[2].As<Napi::Buffer<char>>()).Length() != 16) {
throw Napi::TypeError::New(env, "Second argument must be a 16 byte Buffer");
}
const char minor_ver = ToCharVersion(info[0].As<Napi::String>());
const int32_t rounds = info[1].As<Napi::Number>();
Napi::Buffer<char> seed = info[2].As<Napi::Buffer<char>>();
Napi::Function callback = info[3].As<Napi::Function>();
SaltAsyncWorker* saltWorker = new SaltAsyncWorker(callback, std::string(seed.Data(), 16), rounds, minor_ver);
saltWorker->Queue();
return env.Undefined();
}
Napi::Value GenerateSaltSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 3) {
throw Napi::TypeError::New(env, "3 arguments expected");
}
if (!info[0].IsString()) {
throw Napi::TypeError::New(env, "First argument must be a string");
}
if (!info[2].IsBuffer() || (info[2].As<Napi::Buffer<char>>()).Length() != 16) {
throw Napi::TypeError::New(env, "Third argument must be a 16 byte Buffer");
}
const char minor_ver = ToCharVersion(info[0].As<Napi::String>());
const int32_t rounds = info[1].As<Napi::Number>();
Napi::Buffer<u_int8_t> buffer = info[2].As<Napi::Buffer<u_int8_t>>();
u_int8_t* seed = (u_int8_t*) buffer.Data();
char salt[_SALT_LEN];
bcrypt_gensalt(minor_ver, rounds, seed, salt);
return Napi::String::New(env, salt, strlen(salt));
}
inline std::string BufferToString(const Napi::Buffer<char> &buf) {
return std::string(buf.Data(), buf.Length());
}
/* ENCRYPT DATA - USED TO BE HASHPW */
class EncryptAsyncWorker : public Napi::AsyncWorker {
public:
EncryptAsyncWorker(const Napi::Function& callback, const std::string& input, const std::string& salt)
: Napi::AsyncWorker(callback, "bcrypt:EncryptAsyncWorker"), input(input), salt(salt) {
}
~EncryptAsyncWorker() {}
void Execute() {
if (!(ValidateSalt(salt.c_str()))) {
SetError("Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
}
bcrypt(input.c_str(), input.length(), salt.c_str(), bcrypted);
}
void OnOK() {
Napi::HandleScope scope(Env());
Callback().Call({Env().Undefined(),Napi::String::New(Env(), bcrypted)});
}
private:
std::string input;
std::string salt;
char bcrypted[_PASSWORD_LEN];
};
Napi::Value Encrypt(const Napi::CallbackInfo& info) {
if (info.Length() < 3) {
throw Napi::TypeError::New(info.Env(), "3 arguments expected");
}
std::string data = info[0].IsBuffer()
? BufferToString(info[0].As<Napi::Buffer<char>>())
: info[0].As<Napi::String>();
std::string salt = info[1].As<Napi::String>();
Napi::Function callback = info[2].As<Napi::Function>();
EncryptAsyncWorker* encryptWorker = new EncryptAsyncWorker(callback, data, salt);
encryptWorker->Queue();
return info.Env().Undefined();
}
Napi::Value EncryptSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
throw Napi::TypeError::New(info.Env(), "2 arguments expected");
}
std::string data = info[0].IsBuffer()
? BufferToString(info[0].As<Napi::Buffer<char>>())
: info[0].As<Napi::String>();
std::string salt = info[1].As<Napi::String>();
if (!(ValidateSalt(salt.c_str()))) {
throw Napi::Error::New(env, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
}
char bcrypted[_PASSWORD_LEN];
bcrypt(data.c_str(), data.length(), salt.c_str(), bcrypted);
return Napi::String::New(env, bcrypted, strlen(bcrypted));
}
/* COMPARATOR */
inline bool CompareStrings(const char* s1, const char* s2) {
return strcmp(s1, s2) == 0;
}
class CompareAsyncWorker : public Napi::AsyncWorker {
public:
CompareAsyncWorker(const Napi::Function& callback, const std::string& input, const std::string& encrypted)
: Napi::AsyncWorker(callback, "bcrypt:CompareAsyncWorker"), input(input), encrypted(encrypted) {
result = false;
}
~CompareAsyncWorker() {}
void Execute() {
char bcrypted[_PASSWORD_LEN];
if (ValidateSalt(encrypted.c_str())) {
bcrypt(input.c_str(), input.length(), encrypted.c_str(), bcrypted);
result = CompareStrings(bcrypted, encrypted.c_str());
}
}
void OnOK() {
Napi::HandleScope scope(Env());
Callback().Call({Env().Undefined(), Napi::Boolean::New(Env(), result)});
}
private:
std::string input;
std::string encrypted;
bool result;
};
Napi::Value Compare(const Napi::CallbackInfo& info) {
if (info.Length() < 3) {
throw Napi::TypeError::New(info.Env(), "3 arguments expected");
}
std::string input = info[0].IsBuffer()
? BufferToString(info[0].As<Napi::Buffer<char>>())
: info[0].As<Napi::String>();
std::string encrypted = info[1].As<Napi::String>();
Napi::Function callback = info[2].As<Napi::Function>();
CompareAsyncWorker* compareWorker = new CompareAsyncWorker(callback, input, encrypted);
compareWorker->Queue();
return info.Env().Undefined();
}
Napi::Value CompareSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
throw Napi::TypeError::New(info.Env(), "2 arguments expected");
}
std::string pw = info[0].IsBuffer()
? BufferToString(info[0].As<Napi::Buffer<char>>())
: info[0].As<Napi::String>();
std::string hash = info[1].As<Napi::String>();
char bcrypted[_PASSWORD_LEN];
if (ValidateSalt(hash.c_str())) {
bcrypt(pw.c_str(), pw.length(), hash.c_str(), bcrypted);
return Napi::Boolean::New(env, CompareStrings(bcrypted, hash.c_str()));
} else {
return Napi::Boolean::New(env, false);
}
}
Napi::Value GetRounds(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::TypeError::New(env, "1 argument expected");
}
std::string hash = info[0].As<Napi::String>();
u_int32_t rounds;
if (!(rounds = bcrypt_get_rounds(hash.c_str()))) {
throw Napi::Error::New(env, "invalid hash provided");
}
return Napi::Number::New(env, rounds);
}
} // anonymous namespace
Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "gen_salt_sync"), Napi::Function::New(env, GenerateSaltSync));
exports.Set(Napi::String::New(env, "encrypt_sync"), Napi::Function::New(env, EncryptSync));
exports.Set(Napi::String::New(env, "compare_sync"), Napi::Function::New(env, CompareSync));
exports.Set(Napi::String::New(env, "get_rounds"), Napi::Function::New(env, GetRounds));
exports.Set(Napi::String::New(env, "gen_salt"), Napi::Function::New(env, GenerateSalt));
exports.Set(Napi::String::New(env, "encrypt"), Napi::Function::New(env, Encrypt));
exports.Set(Napi::String::New(env, "compare"), Napi::Function::New(env, Compare));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, init)