1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implement a hashing function.

This commit is contained in:
Sandu Liviu Catalin 2021-01-31 21:50:10 +02:00
parent 2a5dbb1c0d
commit fe27504972
6 changed files with 43 additions and 26 deletions

View File

@ -58,7 +58,6 @@ add_library(SqModule MODULE SqBase.hpp Main.cpp
Library/Chrono/Time.cpp Library/Chrono/Time.hpp
Library/Chrono/Timer.cpp Library/Chrono/Timer.hpp
Library/Chrono/Timestamp.cpp Library/Chrono/Timestamp.hpp
Library/Crypt.cpp Library/Crypt.hpp
Library/CURL.cpp Library/CURL.hpp
Library/IO.cpp Library/IO.hpp
Library/IO/Buffer.cpp Library/IO/Buffer.hpp

View File

@ -1,13 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Crypt.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ================================================================================================
void Register_Crypt(HSQUIRRELVM vm)
{
}
} // Namespace:: SqMod

View File

@ -1,9 +0,0 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Core/Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
} // Namespace:: SqMod

View File

@ -6,12 +6,50 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
static SQInteger SqGetHash(HSQUIRRELVM vm)
{
// Attempt to retrieve the digest from the stack as a string
StackStrF dig(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(dig.Proc(false)))
{
return dig.mRes; // Propagate the error!
}
// Attempt to retrieve the value from the stack as a string
StackStrF val(vm, 3);
// Have we failed to retrieve the string?
if (SQ_FAILED(val.Proc(true)))
{
return val.mRes; // Propagate the error!
}
// Prevent any exceptions from reach the VM
try
{
// Create the digest engine instance
Poco::Crypto::DigestEngine dg(dig.ToStr());
// Attempt to digest the given value
dg.update(val.mPtr, static_cast< size_t >(val.mLen));
// Retrieve the data as hex string
std::string hex = Poco::Crypto::DigestEngine::digestToHex(dg.digest());
// Push the result on the stack
Var< String >::push(vm, hex);
}
catch (const std::exception & e)
{
return sq_throwerrorf(vm, _SC("Failed to hash: %s"), e.what());
}
// At this point we have a valid string on the stack
return 1;
}
// ================================================================================================
void Register_POCO_Crypto(HSQUIRRELVM vm)
{
Table ns(vm);
ns.SquirrelFunc(_SC("Hash"), &SqGetHash);
RootTable(vm).Bind(_SC("SqCrypto"), ns);
}

View File

@ -3,9 +3,11 @@
// ------------------------------------------------------------------------------------------------
#include "Core/Common.hpp"
// ------------------------------------------------------------------------------------------------
#include "Poco/Crypto/DigestEngine.h"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
} // Namespace:: SqMod

View File

@ -32,7 +32,6 @@ extern void Register_CVehicle(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_Chrono(HSQUIRRELVM vm);
extern void Register_Crypt(HSQUIRRELVM vm);
extern void Register_CURL(HSQUIRRELVM vm);
extern void Register_IO(HSQUIRRELVM vm);
extern void Register_Job(HSQUIRRELVM vm);
@ -48,6 +47,7 @@ extern void Register_Worker(HSQUIRRELVM vm);
extern void Register_Web(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
extern void Register_POCO_Crypto(HSQUIRRELVM vm);
extern void Register_POCO_Data(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------
@ -89,7 +89,6 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_CVehicle(vm);
Register_Chrono(vm);
Register_Crypt(vm);
Register_CURL(vm);
Register_IO(vm);
//Register_Job(vm);
@ -104,6 +103,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
//Register_Worker(vm);
//Register_Web(vm);
Register_POCO_Crypto(vm);
Register_POCO_Data(vm);
Register_Constants(vm);