@@ -178,16 +178,23 @@ int CLuaCryptDefs::Base64decode ( lua_State* luaVM )
178
178
179
179
int CLuaCryptDefs::PasswordHash (lua_State* luaVM)
180
180
{
181
- // string password_hash(string password, string algorithm, table options = {})
181
+ // string password_hash(string password, string algorithm, table options = {} [, function callback] )
182
182
SString password;
183
183
PasswordHashFunction algorithm;
184
184
std::unordered_map<SString, SString> options;
185
+ CLuaFunctionRef luaFunctionRef;
185
186
186
187
CScriptArgReader argStream (luaVM);
187
188
argStream.ReadString (password);
188
189
argStream.ReadEnumString (algorithm);
189
190
argStream.ReadStringMap (options, true );
190
191
192
+ if (argStream.NextIsFunction ())
193
+ {
194
+ argStream.ReadFunction (luaFunctionRef);
195
+ argStream.ReadFunctionComplete ();
196
+ }
197
+
191
198
if (!argStream.HasErrors ())
192
199
{
193
200
if (algorithm == PasswordHashFunction::Bcrypt)
@@ -202,14 +209,45 @@ int CLuaCryptDefs::PasswordHash(lua_State* luaVM)
202
209
203
210
if (!ss.fail ())
204
211
{
205
- SString hash = SharedUtil::BcryptHash (password, options[" salt" ], cost);
206
- if (!hash.empty ())
212
+ // Sync
213
+ if (luaFunctionRef == CLuaFunctionRef{})
214
+ {
215
+ SString hash = SharedUtil::BcryptHash (password, options[" salt" ], cost);
216
+ if (!hash.empty ())
217
+ {
218
+ lua_pushstring (luaVM, hash);
219
+ return 1 ;
220
+ }
221
+ else
222
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'salt'" );
223
+ }
224
+ else // Async
207
225
{
208
- lua_pushstring (luaVM, hash);
209
- return 1 ;
226
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
227
+ if (pLuaMain)
228
+ {
229
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask <SString>([password, salt = options[" salt" ], cost] {
230
+ // Execute time-consuming task
231
+ return SharedUtil::BcryptHash (password, salt, cost);
232
+
233
+ }, [luaFunctionRef, pLuaMain](const SString& hash) {
234
+ CLuaArguments arguments;
235
+
236
+ if (hash.empty ())
237
+ {
238
+ m_pScriptDebugging->LogCustom (pLuaMain->GetVM (), " Invalid value for field 'salt'" );
239
+ arguments.PushBoolean (false );
240
+ }
241
+ else
242
+ arguments.PushString (hash);
243
+
244
+ arguments.Call (pLuaMain, luaFunctionRef);
245
+ });
246
+
247
+ lua_pushboolean (luaVM, true );
248
+ return 1 ;
249
+ }
210
250
}
211
- else
212
- m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'salt'" );
213
251
}
214
252
else
215
253
m_pScriptDebugging->LogWarning (luaVM, " Invalid value for field 'cost'" );
@@ -224,19 +262,49 @@ int CLuaCryptDefs::PasswordHash(lua_State* luaVM)
224
262
225
263
int CLuaCryptDefs::PasswordVerify (lua_State* luaVM)
226
264
{
227
- // bool passwordVerify(string password, string hash)
265
+ // bool passwordVerify(string password, string hash [, function callback] )
228
266
SString password;
229
267
SString hash;
268
+ CLuaFunctionRef luaFunctionRef;
230
269
231
270
CScriptArgReader argStream (luaVM);
232
271
argStream.ReadString (password);
233
272
argStream.ReadString (hash);
234
273
274
+ if (argStream.NextIsFunction ())
275
+ {
276
+ argStream.ReadFunction (luaFunctionRef);
277
+ argStream.ReadFunctionComplete ();
278
+ }
279
+
235
280
if (!argStream.HasErrors ())
236
281
{
237
282
if (hash.BeginsWith (" $2y$" ))
238
283
{
239
- lua_pushboolean (luaVM, SharedUtil::BcryptVerify (password, hash));
284
+ // Sync
285
+ if (luaFunctionRef == CLuaFunctionRef ())
286
+ {
287
+ lua_pushboolean (luaVM, SharedUtil::BcryptVerify (password, hash));
288
+ }
289
+ else // Async
290
+ {
291
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
292
+ if (pLuaMain)
293
+ {
294
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask <bool >([password, hash]{
295
+ // Execute time-consuming task
296
+ return SharedUtil::BcryptVerify (password, hash);
297
+
298
+ }, [luaFunctionRef, pLuaMain](const bool & correct) {
299
+ CLuaArguments arguments;
300
+ arguments.PushBoolean (correct);
301
+
302
+ arguments.Call (pLuaMain, luaFunctionRef);
303
+ });
304
+
305
+ lua_pushboolean (luaVM, true );
306
+ }
307
+ }
240
308
return 1 ;
241
309
}
242
310
else
0 commit comments