1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-14 03:37:16 +01:00
SqMod/source/Library/Hashing.cpp

104 lines
3.5 KiB
C++
Raw Normal View History

// ------------------------------------------------------------------------------------------------
2015-10-30 05:37:55 +01:00
#include "Library/Hashing.hpp"
#include "Base/Shared.hpp"
2015-09-30 02:56:11 +02:00
2015-11-09 06:36:41 +01:00
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
2015-10-30 05:37:55 +01:00
// ------------------------------------------------------------------------------------------------
#include <crc32.h>
#include <keccak.h>
#include <md5.h>
#include <sha1.h>
#include <sha256.h>
#include <sha3.h>
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
2015-11-09 06:36:41 +01:00
/* ------------------------------------------------------------------------------------------------
* ...
*/
template < class T > struct BaseHash
2015-10-30 05:37:55 +01:00
{
2015-11-09 06:36:41 +01:00
static T Algo;
};
2015-10-30 05:37:55 +01:00
// ------------------------------------------------------------------------------------------------
2015-11-09 06:36:41 +01:00
template < class T > T BaseHash< T >::Algo;
2015-10-30 05:37:55 +01:00
// ------------------------------------------------------------------------------------------------
2015-11-09 06:36:41 +01:00
template < class T > static SQInteger HashF(HSQUIRRELVM vm)
2015-10-30 05:37:55 +01:00
{
const Int32 top = sq_gettop(vm);
Object ret;
2015-11-09 06:36:41 +01:00
if (top <= 1)
SqThrow("Missing the hash string");
2015-11-09 06:36:41 +01:00
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CCStr str = 0;
if (SQ_FAILED(sq_getstring(vm, -1, &str)))
SqThrow("Unable to retrieve the string");
2015-11-09 06:36:41 +01:00
else
ret = MakeObject(vm, BaseHash< T >::Algo(str));
sq_settop(vm, top);
2015-11-09 06:36:41 +01:00
}
else if (top > 2)
{
CStr str = NULL;
2015-11-09 06:36:41 +01:00
SQInteger len = 0;
if (SQ_FAILED(sqstd_format(vm, 2, &len, &str)))
SqThrow("Unable to generate the string [%s]", Error::Message(vm).c_str());
2015-11-09 06:36:41 +01:00
else
ret = MakeObject(vm, BaseHash< T >::Algo(str));
2015-11-09 06:36:41 +01:00
}
else
SqThrow("Unable to extract the hash string");
Var< Object >::push(vm, ret);
2015-11-09 06:36:41 +01:00
return 1;
2015-10-30 05:37:55 +01:00
}
// ================================================================================================
template < class T > static void RegisterWrapper(Table & hashns, CCStr cname)
2015-10-30 05:37:55 +01:00
{
typedef HashWrapper< T > Hash;
hashns.Bind(cname, Class< Hash >(hashns.GetVM(), cname)
2015-10-30 05:37:55 +01:00
/* Constructors */
.Ctor()
/* Metamethods */
.Func(_SC("_tostring"), &Hash::ToString)
/* Properties */
.Prop(_SC("Hash"), &Hash::GetHash)
2015-10-30 05:37:55 +01:00
/* Functions */
.Func(_SC("Reset"), &Hash::Reset)
.Func(_SC("Compute"), &Hash::Compute)
.Func(_SC("GetHash"), &Hash::GetHash)
.Func(_SC("Add"), &Hash::AddStr)
.Func(_SC("AddStr"), &Hash::AddStr)
2015-10-30 05:37:55 +01:00
);
}
// ================================================================================================
void Register_Hash(HSQUIRRELVM vm)
2015-09-30 02:56:11 +02:00
{
Table hashns(vm);
RegisterWrapper< CRC32 >(hashns, _SC("CRC32"));
RegisterWrapper< Keccak >(hashns, _SC("Keccak"));
RegisterWrapper< MD5 >(hashns, _SC("MD5"));
RegisterWrapper< SHA1 >(hashns, _SC("SHA1"));
RegisterWrapper< SHA256 >(hashns, _SC("SHA256"));
RegisterWrapper< SHA3 >(hashns, _SC("SHA3"));
hashns.SquirrelFunc(_SC("GetCRC32"), &HashF< CRC32 >);
hashns.SquirrelFunc(_SC("GetKeccak"), &HashF< Keccak >);
hashns.SquirrelFunc(_SC("GetMD5"), &HashF< MD5 >);
hashns.SquirrelFunc(_SC("GetSHA1"), &HashF< SHA1 >);
hashns.SquirrelFunc(_SC("GetSHA256"), &HashF< SHA256 >);
hashns.SquirrelFunc(_SC("GetSHA3"), &HashF< SHA3 >);
RootTable(vm).Bind(_SC("SqHash"), hashns);
2015-09-30 02:56:11 +02:00
}
2015-10-30 05:37:55 +01:00
} // Namespace:: SqMod