@@ -79,41 +79,28 @@ std::string CLuaCryptDefs::Base64decode(std::string str)
7979 return SharedUtil::Base64decode (str);
8080}
8181
82- std::variant<std::string, bool > CLuaCryptDefs::PasswordHash (lua_State* luaVM, std::string password, PasswordHashFunction algorithm,
83- std::unordered_map<std::string, std::string> options, std::optional<CLuaFunctionRef> luaFunctionRef)
82+ int CLuaCryptDefs::PasswordHash (lua_State* luaVM)
8483{
8584 // string password_hash(string password, string algorithm, table options = {} [, function callback])
86- if (algorithm != PasswordHashFunction::Bcrypt)
87- {
88- throw std::invalid_argument (" Invalid algorithm" );
89- }
90-
91- // Set default value to 10
92- if (options[" cost" ].empty ())
93- options[" cost" ] = " 10" ;
85+ SString password;
86+ PasswordHashFunction algorithm;
87+ CStringMap options;
88+ CLuaFunctionRef luaFunctionRef;
9489
95- std::stringstream ss (options[" cost" ]);
96- std::size_t cost;
97- ss >> cost;
98-
99- if (ss.fail ())
100- throw std::invalid_argument (" Invalid value for field 'cost'" );
90+ CScriptArgReader argStream (luaVM);
91+ argStream.ReadString (password);
92+ argStream.ReadEnumString (algorithm);
93+ argStream.ReadStringMap (options);
10194
102- // Sync
103- if (!luaFunctionRef.has_value ())
95+ if (argStream.NextIsFunction ())
10496 {
105- SString hash = SharedUtil::BcryptHash (password, options[" salt" ], cost);
106- if (!hash.empty ())
107- {
108- return hash;
109- }
110- else
111- throw std::invalid_argument (" Invalid value for field 'salt'" );
97+ argStream.ReadFunction (luaFunctionRef);
98+ argStream.ReadFunctionComplete ();
11299 }
113- else // Async
100+
101+ if (!argStream.HasErrors ())
114102 {
115- CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
116- if (pLuaMain)
103+ if (algorithm == PasswordHashFunction::Bcrypt)
117104 {
118105 // Set default value to 10
119106 if (options[" cost" ].empty ())
@@ -149,26 +136,46 @@ std::variant<std::string, bool> CLuaCryptDefs::PasswordHash(lua_State* luaVM, st
149136 CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
150137 if (pLuaMain)
151138 {
152- CLuaArguments arguments;
139+ CLuaShared::GetAsyncTaskScheduler ()->PushTask <SString>(
140+ [password, salt = options[" salt" ], cost] {
141+ // Execute time-consuming task
142+ return SharedUtil::BcryptHash (password, salt, cost);
143+ },
144+ [luaFunctionRef](const SString& hash) {
145+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
146+ if (pLuaMain)
147+ {
148+ CLuaArguments arguments;
153149
154- if (hash.empty ())
155- {
156- m_pScriptDebugging->LogCustom (pLuaMain->GetVM (), " Invalid value for field 'salt'" );
157- arguments.PushBoolean (false );
158- }
159- else
160- arguments.PushString (hash);
150+ if (hash.empty ())
151+ {
152+ m_pScriptDebugging->LogCustom (pLuaMain->GetVM (), " Invalid value for field 'salt'" );
153+ arguments.PushBoolean (false );
154+ }
155+ else
156+ arguments.PushString (hash);
161157
162- arguments.Call (pLuaMain, luaFunctionRef. value () );
163- }
164- });
158+ arguments.Call (pLuaMain, luaFunctionRef);
159+ }
160+ });
165161
166- return true ;
162+ lua_pushboolean (luaVM, true );
163+ return 1 ;
164+ }
165+ }
166+ }
167+ else
168+ m_pScriptDebugging->LogWarning (luaVM, " Invalid value for field 'cost'" );
167169 }
168- return false ;
169170 }
171+ else
172+ m_pScriptDebugging->LogCustom (luaVM, argStream.GetFullErrorMessage ());
173+
174+ lua_pushboolean (luaVM, false );
175+ return 1 ;
170176}
171177
178+
172179int CLuaCryptDefs::PasswordVerify (lua_State* luaVM)
173180{
174181 // bool passwordVerify(string password, string hash [, table options, function callback])
0 commit comments