1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02: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

@ -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