Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c27fcaa

Browse files
SDrawMarek Kulik
authored and
Marek Kulik
committed
New encryption functions (#184)
1 parent fd3ec5c commit c27fcaa

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

‎Shared/mods/deathmatch/logic/Enums.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ IMPLEMENT_ENUM_CLASS_BEGIN(PasswordHashFunction)
5050
ADD_ENUM(PasswordHashFunction::Bcrypt, "bcrypt")
5151
IMPLEMENT_ENUM_CLASS_END("password-hash-function")
5252

53+
IMPLEMENT_ENUM_CLASS_BEGIN( StringEncryptFunction )
54+
ADD_ENUM ( StringEncryptFunction::TEA, "tea" )
55+
IMPLEMENT_ENUM_CLASS_END( "string-encrypt-function" )
56+
5357
IMPLEMENT_ENUM_BEGIN( ePacketID )
5458
ADD_ENUM1( PACKET_ID_SERVER_JOIN )
5559
ADD_ENUM1( PACKET_ID_SERVER_JOIN_DATA )

‎Shared/mods/deathmatch/logic/Enums.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ DECLARE_ENUM( eEulerRotationOrder );
6565

6666
DECLARE_ENUM( EHashFunction::EHashFunctionType );
6767
DECLARE_ENUM_CLASS(PasswordHashFunction);
68+
DECLARE_ENUM_CLASS( StringEncryptFunction );
6869

6970
DECLARE_ENUM( ePacketID );

‎Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void CLuaCryptDefs::LoadFunctions ( void )
2121
CLuaCFunctions::AddFunction ( "base64Decode", Base64decode );
2222
CLuaCFunctions::AddFunction("passwordHash", PasswordHash);
2323
CLuaCFunctions::AddFunction("passwordVerify", PasswordVerify);
24+
CLuaCFunctions::AddFunction ( "encodeString", EncodeString );
25+
CLuaCFunctions::AddFunction ( "decodeString", DecodeString );
2426
}
2527

2628
int CLuaCryptDefs::Md5 ( lua_State* luaVM )
@@ -325,3 +327,95 @@ int CLuaCryptDefs::PasswordVerify(lua_State* luaVM)
325327
lua_pushboolean(luaVM, false);
326328
return 1;
327329
}
330+
331+
int CLuaCryptDefs::EncodeString ( lua_State* luaVM )
332+
{
333+
StringEncryptFunction algorithm;
334+
SString data;
335+
CStringMap options;
336+
337+
CScriptArgReader argStream ( luaVM );
338+
argStream.ReadEnumString ( algorithm );
339+
argStream.ReadString ( data );
340+
argStream.ReadStringMap ( options );
341+
342+
if ( !argStream.HasErrors () )
343+
{
344+
switch ( algorithm )
345+
{
346+
case StringEncryptFunction::TEA:
347+
{
348+
SString &key = options["key"];
349+
350+
if ( key.empty () )
351+
{
352+
m_pScriptDebugging->LogCustom ( luaVM, "Invalid value for field 'key'" );
353+
lua_pushboolean ( luaVM, false );
354+
return 1;
355+
}
356+
357+
SString result;
358+
SharedUtil::TeaEncode ( data, key, &result );
359+
lua_pushlstring ( luaVM, result, result.length () );
360+
return 1;
361+
}
362+
default:
363+
{
364+
m_pScriptDebugging->LogCustom ( luaVM, "Unknown encryption algorithm" );
365+
lua_pushboolean ( luaVM, false );
366+
return 1;
367+
}
368+
}
369+
}
370+
else
371+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
372+
373+
lua_pushboolean ( luaVM, false );
374+
return 1;
375+
}
376+
377+
int CLuaCryptDefs::DecodeString ( lua_State* luaVM )
378+
{
379+
StringEncryptFunction algorithm;
380+
SString data;
381+
CStringMap options;
382+
383+
CScriptArgReader argStream ( luaVM );
384+
argStream.ReadEnumString ( algorithm );
385+
argStream.ReadString ( data );
386+
argStream.ReadStringMap ( options );
387+
388+
if ( !argStream.HasErrors () )
389+
{
390+
switch ( algorithm )
391+
{
392+
case StringEncryptFunction::TEA:
393+
{
394+
SString &key = options["key"];
395+
396+
if ( key.empty () )
397+
{
398+
m_pScriptDebugging->LogCustom ( luaVM, "Invalid value for field 'key'" );
399+
lua_pushboolean ( luaVM, false );
400+
return 1;
401+
}
402+
403+
SString result;
404+
SharedUtil::TeaDecode ( data, key, &result );
405+
lua_pushlstring ( luaVM, result, result.length () );
406+
return 1;
407+
}
408+
default:
409+
{
410+
m_pScriptDebugging->LogCustom ( luaVM, "Unknown encryption algorithm" );
411+
lua_pushboolean ( luaVM, false );
412+
return 1;
413+
}
414+
}
415+
}
416+
else
417+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
418+
419+
lua_pushboolean ( luaVM, false );
420+
return 1;
421+
}

‎Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ class CLuaCryptDefs : public CLuaDefs
2525
LUA_DECLARE ( Base64decode );
2626
LUA_DECLARE(PasswordHash);
2727
LUA_DECLARE(PasswordVerify);
28+
LUA_DECLARE ( EncodeString );
29+
LUA_DECLARE ( DecodeString );
2830
};

‎Shared/sdk/SharedUtil.Hash.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ enum class PasswordHashFunction
2929
Bcrypt
3030
};
3131

32+
enum class StringEncryptFunction
33+
{
34+
TEA
35+
};
36+
3237
namespace SharedUtil
3338
{
3439
struct MD5

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /