diff options
author | Mattes D <github@xoft.cz> | 2015-02-21 12:19:07 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-02-21 12:19:07 +0100 |
commit | 006b1afa9e08799ffcfc8a6285b036f711a419ea (patch) | |
tree | 405124838229912c86175d8776ff877549f1b04d | |
parent | Merge pull request #1755 from mc-server/NetworkUDP (diff) | |
parent | Added cCryptoHash namespace to Lua API. (diff) | |
download | cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar.gz cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar.bz2 cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar.lz cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar.xz cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.tar.zst cuberite-006b1afa9e08799ffcfc8a6285b036f711a419ea.zip |
Diffstat (limited to '')
-rw-r--r-- | MCServer/Plugins/APIDump/APIDesc.lua | 24 | ||||
-rw-r--r-- | MCServer/Plugins/Debuggers/Debuggers.lua | 60 | ||||
-rw-r--r-- | MCServer/Plugins/Debuggers/Info.lua | 16 | ||||
-rw-r--r-- | src/Bindings/ManualBindings.cpp | 109 |
4 files changed, 190 insertions, 19 deletions
diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index 4c8dbd1e9..61fc69d32 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -685,6 +685,28 @@ end</pre> }, }, -- 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 = { 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"}, 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"}, - 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 = { diff --git a/MCServer/Plugins/Debuggers/Debuggers.lua b/MCServer/Plugins/Debuggers/Debuggers.lua index a46072324..a28ffa552 100644 --- a/MCServer/Plugins/Debuggers/Debuggers.lua +++ b/MCServer/Plugins/Debuggers/Debuggers.lua @@ -1476,7 +1476,7 @@ function HandleWESel(a_Split, a_Player) SelCuboid:Expand(NumBlocks, NumBlocks, 0, 0, NumBlocks, NumBlocks) -- Set the selection: - local IsSuccess = cPluginManager:CallPlugin("WorldEdit", "SetPlayerCuboidSelection", a_Player, SelCuboid) + IsSuccess = cPluginManager:CallPlugin("WorldEdit", "SetPlayerCuboidSelection", a_Player, SelCuboid) if not(IsSuccess) then a_Player:SendMessage(cCompositeChat():SetMessageType(mtFailure):AddTextPart("Cannot adjust selection, WorldEdit reported failure while setting new selection")) return true @@ -1606,17 +1606,36 @@ 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" +-- List of hashing functions to test: +local HashFunctions = +{ + {"md5", md5 }, + {"cCryptoHash.md5", cCryptoHash.md5 }, + {"cCryptoHash.md5HexString", cCryptoHash.md5HexString }, + {"cCryptoHash.sha1", cCryptoHash.sha1 }, + {"cCryptoHash.sha1HexString", cCryptoHash.sha1HexString }, +} + +-- 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 @@ -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 + + + + diff --git a/MCServer/Plugins/Debuggers/Info.lua b/MCServer/Plugins/Debuggers/Info.lua index b96ef3de5..63a4b9177 100644 --- a/MCServer/Plugins/Debuggers/Info.lua +++ b/MCServer/Plugins/Debuggers/Info.lua @@ -200,21 +200,29 @@ g_PluginInfo = ConsoleCommands = { - ["sched"] = + ["hash"] = { - Handler = HandleConsoleSchedule, - HelpString = "Tests the world scheduling", + Handler = HandleConsoleHash, + HelpString = "Tests the crypto hashing functions", }, + ["loadchunk"] = { Handler = HandleConsoleLoadChunk, HelpString = "Loads the specified chunk into memory", }, + ["preparechunk"] = { Handler = HandleConsolePrepareChunk, HelpString = "Prepares the specified chunk completely (load / gen / light)", - } + }, + + ["sched"] = + { + Handler = HandleConsoleSchedule, + HelpString = "Tests the world scheduling", + }, }, -- ConsoleCommands } -- g_PluginInfo diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 30bce6525..69d16ac2b 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -3,8 +3,11 @@ #include "ManualBindings.h" #undef TOLUA_TEMPLATE_BIND +#include <sstream> +#include <iomanip> #include "tolua++/include/tolua++.h" #include "polarssl/md5.h" +#include "polarssl/sha1.h" #include "PluginLua.h" #include "PluginManager.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]; 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, Output); lua_pushlstring(tolua_S, (const char *)Output, ARRAYCOUNT(Output)); 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) { lua_newtable(tolua_S); @@ -3419,6 +3512,12 @@ static int tolua_cCompositeChat_UnderlineUrls(lua_State * tolua_S) void ManualBindings::Bind(lua_State * tolua_S) { 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, "StringSplit", tolua_StringSplit); 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, "Base64Encode", tolua_Base64Encode); 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_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_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); BindNetwork(tolua_S); |