1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-18 19:47:15 +01:00

Implement a format based hashing API.

This commit is contained in:
Sandu Liviu Catalin 2015-11-09 07:36:41 +02:00
parent 8c81a0f2b2
commit 00da988cc2

View File

@ -1,6 +1,9 @@
#include "Library/Hashing.hpp" #include "Library/Hashing.hpp"
#include "Register.hpp" #include "Register.hpp"
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <crc32.h> #include <crc32.h>
#include <keccak.h> #include <keccak.h>
@ -12,48 +15,78 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
static CRC32 g_EncodeCRC32; * ...
static Keccak g_EncodeKeccak; */
static MD5 g_EncodeMD5; template < class T > struct BaseHash
static SHA1 g_EncodeSHA1; {
static SHA256 g_EncodeSHA256; static T Algo;
static SHA3 g_EncodeSHA3; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
String HashCRC32(const String & str) template < class T > T BaseHash< T >::Algo;
{
return g_EncodeCRC32(str);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
String HashKeccak(const String & str) template < class T > static SQInteger HashF(HSQUIRRELVM vm)
{ {
return g_EncodeKeccak(str); const SQInteger top = sq_gettop(vm);
} // Are there any arguments on the stack?
if (top <= 1)
// ------------------------------------------------------------------------------------------------ {
String HashMD5(const String & str) LogErr("Attempting to <hash string> without specifying a value");
{ // Push a null value on the stack
return g_EncodeMD5(str); sq_pushnull(vm);
} }
// Is there a single string or at least something that can convert to a string on the stack?
// ------------------------------------------------------------------------------------------------ else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
String HashSHA1(const String & str) {
{ // Variable where the resulted string will be retrieved
return g_EncodeSHA1(str); const SQChar * msg = 0;
} // Attempt to retrieve the specified string from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
// ------------------------------------------------------------------------------------------------ {
String HashSHA256(const String & str) LogErr("Unable to <hash string> because : the string cannot be retrieved from the stack");
{ // Pop any pushed values pushed to the stack
return g_EncodeSHA256(str); sq_settop(vm, top);
} // Push a null value on the stack
sq_pushnull(vm);
// ------------------------------------------------------------------------------------------------ }
String HashSHA3(const String & str) else
{ {
return g_EncodeSHA3(str); // Pop any pushed values pushed to the stack
sq_settop(vm, top);
// Hash the specified string
sq_pushstring(vm, BaseHash< T >::Algo(msg).c_str(), -1);
}
}
else if (top > 2)
{
// Variables containing the resulted string
SQChar * msg = NULL;
SQInteger len = 0;
// Attempt to call the format function with the passed arguments
if (SQ_FAILED(sqstd_format(vm, 2, &len, &msg)))
{
LogErr("Unable to <hash string> because : %s", Error::Message(vm).c_str());
// Push a null value on the stack
sq_pushnull(vm);
}
else
{
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
// Hash the specified string
sq_pushstring(vm, BaseHash< T >::Algo(msg).c_str(), -1);
}
}
else
{
LogErr("Unable to <extract the log message> from the specified value");
// Push a null value on the stack
sq_pushnull(vm);
}
// At this point everything went correctly (probably)
return 1;
} }
// ================================================================================================ // ================================================================================================
@ -102,12 +135,12 @@ bool Register_Hash(HSQUIRRELVM vm)
LogDbg("Beginning registration of <Hashing functions> type"); LogDbg("Beginning registration of <Hashing functions> type");
// Attempt to register the free functions // Attempt to register the free functions
Sqrat::RootTable(vm) Sqrat::RootTable(vm)
.Func(_SC("HashCRC32"), &HashCRC32) .SquirrelFunc(_SC("HashCRC32"), &HashF< CRC32 >)
.Func(_SC("HashKeccak"), &HashKeccak) .SquirrelFunc(_SC("HashKeccak"), &HashF< Keccak >)
.Func(_SC("HashMD5"), &HashMD5) .SquirrelFunc(_SC("HashMD5"), &HashF< MD5 >)
.Func(_SC("HashSHA1"), &HashSHA1) .SquirrelFunc(_SC("HashSHA1"), &HashF< SHA1 >)
.Func(_SC("HashSHA256"), &HashSHA256) .SquirrelFunc(_SC("HashSHA256"), &HashF< SHA256 >)
.Func(_SC("HashSHA3"), &HashSHA3); .SquirrelFunc(_SC("HashSHA3"), &HashF< SHA3 >);
// Output debugging information // Output debugging information
LogDbg("Registration of <Hashing functions> type was successful"); LogDbg("Registration of <Hashing functions> type was successful");
// Registration succeeded // Registration succeeded