1
0

Added cCryptoHash namespace to Lua API.

This commit is contained in:
Mattes D 2015-02-21 09:41:14 +01:00
parent 22d3a6a47f
commit b9e4fe0a3b
4 changed files with 190 additions and 19 deletions

View File

@ -685,6 +685,28 @@ end</pre>
}, },
}, -- cCraftingRecipe }, -- cCraftingRecipe
cCryptoHash =
{
Desc =
[[
Provides functions for generating cryptographic hashes.</p>
<p>
Note that all functions in this class are static, so they should be called in the dot convention:
<pre class="prettyprint lang-lua">
local Hash = cCryptoHash.sha1HexString("DataToHash")
</pre></p>
<p>Each cryptographic hash has two variants, one returns the hash as a raw binary string, the other returns the hash as a hex-encoded string twice as long as the binary string.
]],
Functions =
{
md5 = { Params = "Data", Return = "string", Notes = "(STATIC) Calculates the md5 hash of the data, returns it as a raw (binary) string of 16 characters." },
md5HexString = { Params = "Data", Return = "string", Notes = "(STATIC) Calculates the md5 hash of the data, returns it as a hex-encoded string of 32 characters." },
sha1 = { Params = "Data", Return = "string", Notes = "(STATIC) Calculates the sha1 hash of the data, returns it as a raw (binary) string of 20 characters." },
sha1HexString = { Params = "Data", Return = "string", Notes = "(STATIC) Calculates the sha1 hash of the data, returns it as a hex-encoded string of 40 characters." },
},
}, -- cCryptoHash
cEnchantments = cEnchantments =
{ {
Desc = [[ Desc = [[
@ -2929,7 +2951,7 @@ end
StringToMobType = {Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "<b>DEPRECATED!</b> Please use cMonster:StringToMobType(). Converts a string representation to a {{Globals#MobType|MobType}} enumerated value"}, StringToMobType = {Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "<b>DEPRECATED!</b> Please use cMonster:StringToMobType(). Converts a string representation to a {{Globals#MobType|MobType}} enumerated value"},
StripColorCodes = {Params = "string", Return = "string", Notes = "Removes all control codes used by MC for colors and styles"}, StripColorCodes = {Params = "string", Return = "string", Notes = "Removes all control codes used by MC for colors and styles"},
TrimString = {Params = "string", Return = "string", Notes = "Trims whitespace at both ends of the string"}, TrimString = {Params = "string", Return = "string", Notes = "Trims whitespace at both ends of the string"},
md5 = {Params = "string", Return = "string", Notes = "converts a string to an md5 hash"}, md5 = {Params = "string", Return = "string", Notes = "<b>OBSOLETE</b>, use the {{cCryptoHash}} functions instead.<br>Converts a string to a raw binary md5 hash."},
}, },
ConstantGroups = ConstantGroups =
{ {

View File

@ -1476,7 +1476,7 @@ function HandleWESel(a_Split, a_Player)
SelCuboid:Expand(NumBlocks, NumBlocks, 0, 0, NumBlocks, NumBlocks) SelCuboid:Expand(NumBlocks, NumBlocks, 0, 0, NumBlocks, NumBlocks)
-- Set the selection: -- Set the selection:
local IsSuccess = cPluginManager:CallPlugin("WorldEdit", "SetPlayerCuboidSelection", a_Player, SelCuboid) IsSuccess = cPluginManager:CallPlugin("WorldEdit", "SetPlayerCuboidSelection", a_Player, SelCuboid)
if not(IsSuccess) then if not(IsSuccess) then
a_Player:SendMessage(cCompositeChat():SetMessageType(mtFailure):AddTextPart("Cannot adjust selection, WorldEdit reported failure while setting new selection")) a_Player:SendMessage(cCompositeChat():SetMessageType(mtFailure):AddTextPart("Cannot adjust selection, WorldEdit reported failure while setting new selection"))
return true return true
@ -1606,17 +1606,36 @@ end
function HandleConsoleSchedule(a_Split) -- List of hashing functions to test:
local prev = os.clock() local HashFunctions =
LOG("Scheduling a task for 2 seconds in the future (current os.clock is " .. prev .. ")") {
cRoot:Get():GetDefaultWorld():ScheduleTask(40, {"md5", md5 },
function () {"cCryptoHash.md5", cCryptoHash.md5 },
local current = os.clock() {"cCryptoHash.md5HexString", cCryptoHash.md5HexString },
local diff = current - prev {"cCryptoHash.sha1", cCryptoHash.sha1 },
LOG("Scheduled function is called. Current os.clock is " .. current .. ", difference is " .. diff .. ")") {"cCryptoHash.sha1HexString", cCryptoHash.sha1HexString },
end }
)
return true, "Task scheduled" -- List of strings to try hashing:
local HashExamples =
{
"",
"\0",
"test",
}
function HandleConsoleHash(a_Split)
for _, str in ipairs(HashExamples) do
LOG("Hashing string \"" .. str .. "\":")
for _, hash in ipairs(HashFunctions) do
if not(hash[2]) then
LOG("Hash function " .. hash[1] .. " doesn't exist in the API!")
else
LOG(hash[1] .. "() = " .. hash[2](str))
end
end -- for hash - HashFunctions[]
end -- for str - HashExamples[]
return true
end end
@ -1704,3 +1723,20 @@ end
function HandleConsoleSchedule(a_Split)
local prev = os.clock()
LOG("Scheduling a task for 2 seconds in the future (current os.clock is " .. prev .. ")")
cRoot:Get():GetDefaultWorld():ScheduleTask(40,
function ()
local current = os.clock()
local diff = current - prev
LOG("Scheduled function is called. Current os.clock is " .. current .. ", difference is " .. diff .. ")")
end
)
return true, "Task scheduled"
end

View File

@ -200,21 +200,29 @@ g_PluginInfo =
ConsoleCommands = ConsoleCommands =
{ {
["sched"] = ["hash"] =
{ {
Handler = HandleConsoleSchedule, Handler = HandleConsoleHash,
HelpString = "Tests the world scheduling", HelpString = "Tests the crypto hashing functions",
}, },
["loadchunk"] = ["loadchunk"] =
{ {
Handler = HandleConsoleLoadChunk, Handler = HandleConsoleLoadChunk,
HelpString = "Loads the specified chunk into memory", HelpString = "Loads the specified chunk into memory",
}, },
["preparechunk"] = ["preparechunk"] =
{ {
Handler = HandleConsolePrepareChunk, Handler = HandleConsolePrepareChunk,
HelpString = "Prepares the specified chunk completely (load / gen / light)", HelpString = "Prepares the specified chunk completely (load / gen / light)",
} },
["sched"] =
{
Handler = HandleConsoleSchedule,
HelpString = "Tests the world scheduling",
},
}, -- ConsoleCommands }, -- ConsoleCommands
} -- g_PluginInfo } -- g_PluginInfo

View File

@ -3,8 +3,11 @@
#include "ManualBindings.h" #include "ManualBindings.h"
#undef TOLUA_TEMPLATE_BIND #undef TOLUA_TEMPLATE_BIND
#include <sstream>
#include <iomanip>
#include "tolua++/include/tolua++.h" #include "tolua++/include/tolua++.h"
#include "polarssl/md5.h" #include "polarssl/md5.h"
#include "polarssl/sha1.h"
#include "PluginLua.h" #include "PluginLua.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "LuaWindow.h" #include "LuaWindow.h"
@ -2203,11 +2206,16 @@ static int tolua_cPlugin_Call(lua_State * tolua_S)
static int tolua_md5(lua_State* tolua_S) static int tolua_md5(lua_State * tolua_S)
{ {
// Calculate the raw md5 checksum byte array:
unsigned char Output[16]; unsigned char Output[16];
size_t len = 0; size_t len = 0;
const unsigned char * SourceString = (const unsigned char *)lua_tolstring(tolua_S, 1, &len); const unsigned char * SourceString = (const unsigned char *)lua_tolstring(tolua_S, 1, &len);
if (SourceString == nullptr)
{
return 0;
}
md5(SourceString, len, Output); md5(SourceString, len, Output);
lua_pushlstring(tolua_S, (const char *)Output, ARRAYCOUNT(Output)); lua_pushlstring(tolua_S, (const char *)Output, ARRAYCOUNT(Output));
return 1; return 1;
@ -2217,6 +2225,91 @@ static int tolua_md5(lua_State* tolua_S)
/** Does the same as tolua_md5, but reports that the usage is obsolete and the plugin should use cCrypto.md5(). */
static int tolua_md5_obsolete(lua_State * tolua_S)
{
LOGWARNING("Using md5() is obsolete, please change your plugin to use cCryptoHash.md5()");
cLuaState::LogStackTrace(tolua_S);
return tolua_md5(tolua_S);
}
static int tolua_md5HexString(lua_State * tolua_S)
{
// Calculate the raw md5 checksum byte array:
unsigned char md5Output[16];
size_t len = 0;
const unsigned char * SourceString = (const unsigned char *)lua_tolstring(tolua_S, 1, &len);
if (SourceString == nullptr)
{
return 0;
}
md5(SourceString, len, md5Output);
// Convert the md5 checksum to hex string:
std::stringstream Output;
Output << std::hex << std::setw(2) << std::setfill('0');
for (size_t i = 0; i < ARRAYCOUNT(md5Output); i++)
{
Output << static_cast<unsigned short>(md5Output[i]); // Need to cast to a number, otherwise a char is output
}
lua_pushlstring(tolua_S, Output.str().c_str(), Output.str().size());
return 1;
}
static int tolua_sha1(lua_State * tolua_S)
{
// Calculate the raw SHA1 checksum byte array from the input string:
unsigned char Output[20];
size_t len = 0;
const unsigned char * SourceString = (const unsigned char *)lua_tolstring(tolua_S, 1, &len);
if (SourceString == nullptr)
{
return 0;
}
sha1(SourceString, len, Output);
lua_pushlstring(tolua_S, (const char *)Output, ARRAYCOUNT(Output));
return 1;
}
static int tolua_sha1HexString(lua_State * tolua_S)
{
// Calculate the raw SHA1 checksum byte array from the input string:
unsigned char sha1Output[20];
size_t len = 0;
const unsigned char * SourceString = (const unsigned char *)lua_tolstring(tolua_S, 1, &len);
if (SourceString == nullptr)
{
return 0;
}
sha1(SourceString, len, sha1Output);
// Convert the sha1 checksum to hex string:
std::stringstream Output;
Output << std::hex << std::setw(2) << std::setfill('0');
for (size_t i = 0; i < ARRAYCOUNT(sha1Output); i++)
{
Output << static_cast<unsigned short>(sha1Output[i]); // Need to cast to a number, otherwise a char is output
}
lua_pushlstring(tolua_S, Output.str().c_str(), Output.str().size());
return 1;
}
static int tolua_push_StringStringMap(lua_State* tolua_S, std::map< std::string, std::string >& a_StringStringMap) static int tolua_push_StringStringMap(lua_State* tolua_S, std::map< std::string, std::string >& a_StringStringMap)
{ {
lua_newtable(tolua_S); lua_newtable(tolua_S);
@ -3419,6 +3512,12 @@ static int tolua_cCompositeChat_UnderlineUrls(lua_State * tolua_S)
void ManualBindings::Bind(lua_State * tolua_S) void ManualBindings::Bind(lua_State * tolua_S)
{ {
tolua_beginmodule(tolua_S, nullptr); tolua_beginmodule(tolua_S, nullptr);
// Create the new classes:
tolua_usertype(tolua_S, "cCryptoHash");
tolua_cclass(tolua_S, "cCryptoHash", "cCryptoHash", "", nullptr);
// Globals:
tolua_function(tolua_S, "Clamp", tolua_Clamp); tolua_function(tolua_S, "Clamp", tolua_Clamp);
tolua_function(tolua_S, "StringSplit", tolua_StringSplit); tolua_function(tolua_S, "StringSplit", tolua_StringSplit);
tolua_function(tolua_S, "StringSplitAndTrim", tolua_StringSplitAndTrim); tolua_function(tolua_S, "StringSplitAndTrim", tolua_StringSplitAndTrim);
@ -3429,6 +3528,7 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "LOGERROR", tolua_LOGERROR); tolua_function(tolua_S, "LOGERROR", tolua_LOGERROR);
tolua_function(tolua_S, "Base64Encode", tolua_Base64Encode); tolua_function(tolua_S, "Base64Encode", tolua_Base64Encode);
tolua_function(tolua_S, "Base64Decode", tolua_Base64Decode); tolua_function(tolua_S, "Base64Decode", tolua_Base64Decode);
tolua_function(tolua_S, "md5", tolua_md5_obsolete); // OBSOLETE, use cCryptoHash.md5() instead
tolua_beginmodule(tolua_S, "cFile"); tolua_beginmodule(tolua_S, "cFile");
tolua_function(tolua_S, "GetFolderContents", tolua_cFile_GetFolderContents); tolua_function(tolua_S, "GetFolderContents", tolua_cFile_GetFolderContents);
@ -3585,7 +3685,12 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "GetSlotCoords", Lua_ItemGrid_GetSlotCoords); tolua_function(tolua_S, "GetSlotCoords", Lua_ItemGrid_GetSlotCoords);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);
tolua_function(tolua_S, "md5", tolua_md5); tolua_beginmodule(tolua_S, "cCryptoHash");
tolua_function(tolua_S, "md5", tolua_md5);
tolua_function(tolua_S, "md5HexString", tolua_md5HexString);
tolua_function(tolua_S, "sha1", tolua_sha1);
tolua_function(tolua_S, "sha1HexString", tolua_sha1HexString);
tolua_endmodule(tolua_S);
BindRankManager(tolua_S); BindRankManager(tolua_S);
BindNetwork(tolua_S); BindNetwork(tolua_S);