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

Implemented the hashing library.

This commit is contained in:
Sandu Liviu Catalin 2015-10-30 06:37:55 +02:00
parent f27dea4693
commit ce5957395a
4 changed files with 190 additions and 5 deletions

View File

@ -1,13 +1,117 @@
#include "Library/CFG.hpp"
#include "Library/Hashing.hpp"
#include "Register.hpp"
// ------------------------------------------------------------------------------------------------
#include <crc32.h>
#include <keccak.h>
#include <md5.h>
#include <sha1.h>
#include <sha256.h>
#include <sha3.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
bool Register_CFG(HSQUIRRELVM vm)
static CRC32 g_EncodeCRC32;
static Keccak g_EncodeKeccak;
static MD5 g_EncodeMD5;
static SHA1 g_EncodeSHA1;
static SHA256 g_EncodeSHA256;
static SHA3 g_EncodeSHA3;
// ------------------------------------------------------------------------------------------------
String HashCRC32(const String & str) noexcept
{
return g_EncodeCRC32(str);
}
// ------------------------------------------------------------------------------------------------
String HashKeccak(const String & str) noexcept
{
return g_EncodeKeccak(str);
}
// ------------------------------------------------------------------------------------------------
String HashMD5(const String & str) noexcept
{
return g_EncodeMD5(str);
}
// ------------------------------------------------------------------------------------------------
String HashSHA1(const String & str) noexcept
{
return g_EncodeSHA1(str);
}
// ------------------------------------------------------------------------------------------------
String HashSHA256(const String & str) noexcept
{
return g_EncodeSHA256(str);
}
// ------------------------------------------------------------------------------------------------
String HashSHA3(const String & str) noexcept
{
return g_EncodeSHA3(str);
}
// ================================================================================================
template < class T > static bool RegisterWrapper(HSQUIRRELVM vm, const SQChar * cname)
{
// Typedef the reference type to simplify code
typedef HashWrapper< T > Hash;
// Output debugging information
LogDbg("Beginning registration of <%s> type", cname);
// Attempt to register the specified type
Sqrat::RootTable(vm).Bind(cname, Sqrat::Class< Hash >(vm, cname)
/* Constructors */
.Ctor()
/* Metamethods */
.Func(_SC("_tostring"), &Hash::ToString)
/* Properties */
.Prop(_SC("hash"), &Hash::GetHash)
/* Functions */
.Func(_SC("reset"), &Hash::Reset)
.Func(_SC("compute"), &Hash::Compute)
.Func(_SC("get_hash"), &Hash::GetHash)
.Func(_SC("add"), &Hash::AddStr)
.Func(_SC("add_str"), &Hash::AddStr)
);
// Output debugging information
LogDbg("Registration of <%s> type was successful", cname);
// Registration succeeded
return true;
}
} // Namespace:: SqMod
// ------------------------------------------------------------------------------------------------
bool Register_Hash(HSQUIRRELVM vm)
{
// Attempt to register the hash wrapers
if (!RegisterWrapper< CRC32 >(vm, _SC("CHashCRC32")) ||
!RegisterWrapper< Keccak >(vm, _SC("CHashKeccak")) ||
!RegisterWrapper< MD5 >(vm, _SC("CHashMD5")) ||
!RegisterWrapper< SHA1 >(vm, _SC("CHashSHA1")) ||
!RegisterWrapper< SHA256 >(vm, _SC("CHashSHA256")) ||
!RegisterWrapper< SHA3 >(vm, _SC("CHashSHA3")))
{
return false;
}
// Output debugging information
LogDbg("Beginning registration of <Hashing functions> type");
// Attempt to register the free functions
Sqrat::RootTable(vm)
.Func(_SC("HashCRC32"), &HashCRC32)
.Func(_SC("HashKeccak"), &HashKeccak)
.Func(_SC("HashMD5"), &HashMD5)
.Func(_SC("HashSHA1"), &HashSHA1)
.Func(_SC("HashSHA256"), &HashSHA256)
.Func(_SC("HashSHA3"), &HashSHA3);
// Output debugging information
LogDbg("Registration of <Hashing functions> type was successful");
// Registration succeeded
return true;
}
} // Namespace:: SqMod

View File

@ -2,7 +2,7 @@
#define _LIBRARY_CFG_HPP_
// ------------------------------------------------------------------------------------------------
#include "Config.hpp"
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -10,7 +10,86 @@ namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* ...
*/
template < class T > class HashWrapper
{
public:
/* --------------------------------------------------------------------------------------------
* ...
*/
HashWrapper() = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
HashWrapper(const HashWrapper & o) = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
HashWrapper(HashWrapper && o) = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
~HashWrapper() = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
HashWrapper & operator = (const HashWrapper & o) = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
HashWrapper & operator = (HashWrapper && o) = default;
/* --------------------------------------------------------------------------------------------
* ...
*/
String ToString() noexcept
{
return m_Encoder.getHash();
}
/* --------------------------------------------------------------------------------------------
* ...
*/
void Reset() noexcept
{
m_Encoder.reset();
}
/* --------------------------------------------------------------------------------------------
* ...
*/
String Compute(const String & str) noexcept
{
return m_Encoder(str);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
String GetHash() noexcept
{
return m_Encoder.getHash();
}
/* --------------------------------------------------------------------------------------------
* ...
*/
void AddStr(const String & str) noexcept
{
m_Encoder.add(str.data(), str.length() * sizeof(String::value_type));
}
private:
// --------------------------------------------------------------------------------------------
T m_Encoder;
};
} // Namespace:: SqMod
#endif // _LIBRARY_CFG_HPP_
#endif // _LIBRARY_CFG_HPP_

View File

@ -43,6 +43,7 @@ bool RegisterAPI(HSQUIRRELVM vm) noexcept
_Log->cFtl(!Register_Datetime(vm), "Unable to register: Datetime") || \
_Log->cFtl(!Register_FileIO(vm), "Unable to register: FileIO") || \
_Log->cFtl(!Register_Format(vm), "Unable to register: Format") || \
_Log->cFtl(!Register_Hash(vm), "Unable to register: Hash") || \
_Log->cFtl(!Register_IRC(vm), "Unable to register: IRC") || \
_Log->cFtl(!Register_INI(vm), "Unable to register: INI") || \
_Log->cFtl(!Register_JSON(vm), "Unable to register: JSON") || \

View File

@ -44,6 +44,7 @@ bool Register_Entity(HSQUIRRELVM vm);
bool Register_Datetime(HSQUIRRELVM vm);
bool Register_FileIO(HSQUIRRELVM vm);
bool Register_Format(HSQUIRRELVM vm);
bool Register_Hash(HSQUIRRELVM vm);
bool Register_IRC(HSQUIRRELVM vm);
bool Register_INI(HSQUIRRELVM vm);
bool Register_JSON(HSQUIRRELVM vm);