Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base32 & Base64 on encodeString #2016

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions Shared/mods/deathmatch/logic/Enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ IMPLEMENT_ENUM_CLASS_END("password-hash-function")

IMPLEMENT_ENUM_CLASS_BEGIN(StringEncryptFunction)
ADD_ENUM(StringEncryptFunction::TEA, "tea")
ADD_ENUM(StringEncryptFunction::BASE32, "base32")
ADD_ENUM(StringEncryptFunction::BASE64, "base64")
IMPLEMENT_ENUM_CLASS_END("string-encrypt-function")

IMPLEMENT_ENUM_BEGIN(ePacketID)
Expand Down
24 changes: 22 additions & 2 deletions Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,19 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
}
return 1;
}
case StringEncryptFunction::BASE32:
{
lua::Push(luaVM, SharedUtil::Base32encode(data));
return 1;
}
case StringEncryptFunction::BASE64:
{
lua::Push(luaVM, SharedUtil::Base64encode(data));
return 1;
}
default:
{
m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm");
m_pScriptDebugging->LogCustom(luaVM, "Unknown algorithm");
lua_pushboolean(luaVM, false);
return 1;
}
Expand Down Expand Up @@ -397,9 +407,19 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
}
return 1;
}
case StringEncryptFunction::BASE32:
{
lua::Push(luaVM, SharedUtil::Base32decode(data));
return 1;
}
case StringEncryptFunction::BASE64:
{
lua::Push(luaVM, SharedUtil::Base64decode(data));
return 1;
}
default:
{
m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm");
m_pScriptDebugging->LogCustom(luaVM, "Unknown algorithm");
lua_pushboolean(luaVM, false);
return 1;
}
Expand Down
17 changes: 17 additions & 0 deletions Shared/sdk/SharedUtil.Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*****************************************************************************/
#pragma once
#include <cryptopp/base64.h>
#include <cryptopp/base32.h>
#include "SString.h"

namespace SharedUtil
Expand All @@ -28,4 +29,20 @@ namespace SharedUtil

return result;
}

inline SString Base32encode(const SString& data)
{
SString result;
CryptoPP::StringSource ss(data, true, new CryptoPP::Base32Encoder(new CryptoPP::StringSink(result), false)); // Memory is freed automatically

return result;
}

inline SString Base32decode(const SString& data)
{
SString result;
CryptoPP::StringSource ss(data, true, new CryptoPP::Base32Decoder(new CryptoPP::StringSink(result))); // Memory is freed automatically

return result;
}
} // namespace SharedUtil
4 changes: 3 additions & 1 deletion Shared/sdk/SharedUtil.Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ enum class PasswordHashFunction

enum class StringEncryptFunction
{
TEA
TEA,
BASE32,
BASE64
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The StringEncryptFunction name is a bit odd due to base64 not being an encryption method. Maybe we should change this to StringEncodeFunction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review. I'll edit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cleopatradev This change is still pending.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cleopatradev This is still an issue.


namespace SharedUtil
Expand Down