mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-16 07:07:13 +02:00
Rename source to module.
This commit is contained in:
69
module/Vendor/Hash/include/crc32.h
vendored
Normal file
69
module/Vendor/Hash/include/crc32.h
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// crc32.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute CRC32 hash, based on Intel's Slicing-by-8 algorithm
|
||||
/** Usage:
|
||||
CRC32 crc32;
|
||||
std::string myHash = crc32("Hello World"); // std::string
|
||||
std::string myHash2 = crc32("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
CRC32 crc32;
|
||||
while (more data available)
|
||||
crc32.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = crc32.getHash();
|
||||
|
||||
Note:
|
||||
You can find code for the faster Slicing-by-16 algorithm on my website, too:
|
||||
http://create.stephan-brumme.com/crc32/
|
||||
Its unrolled version is about twice as fast but its look-up table doubled in size as well.
|
||||
*/
|
||||
class CRC32 //: public Hash
|
||||
{
|
||||
public:
|
||||
/// hash is 4 bytes long
|
||||
enum { HashBytes = 4 };
|
||||
|
||||
/// same as reset()
|
||||
CRC32();
|
||||
|
||||
/// compute CRC32 of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute CRC32 of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as 8 hex characters
|
||||
std::string getHash();
|
||||
/// return latest hash as bytes
|
||||
void getHash(unsigned char buffer[HashBytes]);
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// hash
|
||||
uint32_t m_hash;
|
||||
};
|
28
module/Vendor/Hash/include/hash.h
vendored
Normal file
28
module/Vendor/Hash/include/hash.h
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// hash.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/// abstract base class
|
||||
class Hash
|
||||
{
|
||||
public:
|
||||
/// compute hash of a memory block
|
||||
virtual std::string operator()(const void* data, size_t numBytes) = 0;
|
||||
/// compute hash of a string, excluding final zero
|
||||
virtual std::string operator()(const std::string& text) = 0;
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
virtual void add(const void* data, size_t numBytes) = 0;
|
||||
|
||||
/// return latest hash as hex characters
|
||||
virtual std::string getHash() = 0;
|
||||
|
||||
/// restart
|
||||
virtual void reset() = 0;
|
||||
};
|
83
module/Vendor/Hash/include/hmac.h
vendored
Normal file
83
module/Vendor/Hash/include/hmac.h
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// hmac.h
|
||||
// Copyright (c) 2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
// based on http://tools.ietf.org/html/rfc2104
|
||||
// see also http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
|
||||
|
||||
/** Usage:
|
||||
std::string msg = "The quick brown fox jumps over the lazy dog";
|
||||
std::string key = "key";
|
||||
std::string md5hmac = hmac< MD5 >(msg, key);
|
||||
std::string sha1hmac = hmac< SHA1 >(msg, key);
|
||||
std::string sha2hmac = hmac<SHA256>(msg, key);
|
||||
|
||||
Note:
|
||||
To keep my code simple, HMAC computation currently needs the whole message at once.
|
||||
This is in contrast to the hashes MD5, SHA1, etc. where an add() method is available
|
||||
for incremental computation.
|
||||
You can use any hash for HMAC as long as it provides:
|
||||
- constant HashMethod::BlockSize (typically 64)
|
||||
- constant HashMethod::HashBytes (length of hash in bytes, e.g. 20 for SHA1)
|
||||
- HashMethod::add(buffer, bufferSize)
|
||||
- HashMethod::getHash(unsigned char buffer[HashMethod::BlockSize])
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <cstring> // memcpy
|
||||
|
||||
/// compute HMAC hash of data and key using MD5, SHA1 or SHA256
|
||||
template <typename HashMethod>
|
||||
std::string hmac(const void* data, size_t numDataBytes, const void* key, size_t numKeyBytes)
|
||||
{
|
||||
// initialize key with zeros
|
||||
unsigned char usedKey[HashMethod::BlockSize] = {0};
|
||||
|
||||
// adjust length of key: must contain exactly blockSize bytes
|
||||
if (numKeyBytes <= HashMethod::BlockSize)
|
||||
{
|
||||
// copy key
|
||||
memcpy(usedKey, key, numKeyBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
// shorten key: usedKey = hashed(key)
|
||||
HashMethod keyHasher;
|
||||
keyHasher.add(key, numKeyBytes);
|
||||
keyHasher.getHash(usedKey);
|
||||
}
|
||||
|
||||
// create initial XOR padding
|
||||
for (size_t i = 0; i < HashMethod::BlockSize; i++)
|
||||
usedKey[i] ^= 0x36;
|
||||
|
||||
// inside = hash((usedKey ^ 0x36) + data)
|
||||
unsigned char inside[HashMethod::HashBytes];
|
||||
HashMethod insideHasher;
|
||||
insideHasher.add(usedKey, HashMethod::BlockSize);
|
||||
insideHasher.add(data, numDataBytes);
|
||||
insideHasher.getHash(inside);
|
||||
|
||||
// undo usedKey's previous 0x36 XORing and apply a XOR by 0x5C
|
||||
for (size_t i = 0; i < HashMethod::BlockSize; i++)
|
||||
usedKey[i] ^= 0x5C ^ 0x36;
|
||||
|
||||
// hash((usedKey ^ 0x5C) + hash((usedKey ^ 0x36) + data))
|
||||
HashMethod finalHasher;
|
||||
finalHasher.add(usedKey, HashMethod::BlockSize);
|
||||
finalHasher.add(inside, HashMethod::HashBytes);
|
||||
|
||||
return finalHasher.getHash();
|
||||
}
|
||||
|
||||
|
||||
/// convenience function for std::string
|
||||
template <typename HashMethod>
|
||||
std::string hmac(const std::string& data, const std::string& key)
|
||||
{
|
||||
return hmac<HashMethod>(data.c_str(), data.size(), key.c_str(), key.size());
|
||||
}
|
81
module/Vendor/Hash/include/keccak.h
vendored
Normal file
81
module/Vendor/Hash/include/keccak.h
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// keccak.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute Keccak hash (designated SHA3)
|
||||
/** Usage:
|
||||
Keccak keccak;
|
||||
std::string myHash = keccak("Hello World"); // std::string
|
||||
std::string myHash2 = keccak("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
Keccak keccak;
|
||||
while (more data available)
|
||||
keccak.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = keccak.getHash();
|
||||
*/
|
||||
class Keccak //: public Hash
|
||||
{
|
||||
public:
|
||||
/// algorithm variants
|
||||
enum Bits { Keccak224 = 224, Keccak256 = 256, Keccak384 = 384, Keccak512 = 512 };
|
||||
|
||||
/// same as reset()
|
||||
explicit Keccak(Bits bits = Keccak256);
|
||||
|
||||
/// compute hash of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute hash of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as hex characters
|
||||
std::string getHash();
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// process a full block
|
||||
void processBlock(const void* data);
|
||||
/// process everything left in the internal buffer
|
||||
void processBuffer();
|
||||
|
||||
/// 1600 bits, stored as 25x64 bit, BlockSize is no more than 1152 bits (Keccak224)
|
||||
enum { StateSize = 1600 / (8 * 8),
|
||||
MaxBlockSize = 200 - 2 * (224 / 8) };
|
||||
|
||||
/// hash
|
||||
uint64_t m_hash[StateSize];
|
||||
/// size of processed data in bytes
|
||||
uint64_t m_numBytes;
|
||||
/// block size (less or equal to MaxBlockSize)
|
||||
size_t m_blockSize;
|
||||
/// valid bytes in m_buffer
|
||||
size_t m_bufferSize;
|
||||
/// bytes not processed yet
|
||||
uint8_t m_buffer[MaxBlockSize];
|
||||
/// variant
|
||||
Bits m_bits;
|
||||
};
|
78
module/Vendor/Hash/include/md5.h
vendored
Normal file
78
module/Vendor/Hash/include/md5.h
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// md5.h
|
||||
// Copyright (c) 2014 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute MD5 hash
|
||||
/** Usage:
|
||||
MD5 md5;
|
||||
std::string myHash = md5("Hello World"); // std::string
|
||||
std::string myHash2 = md5("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
MD5 md5;
|
||||
while (more data available)
|
||||
md5.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = md5.getHash();
|
||||
*/
|
||||
class MD5 //: public Hash
|
||||
{
|
||||
public:
|
||||
/// split into 64 byte blocks (=> 512 bits), hash is 16 bytes long
|
||||
enum { BlockSize = 512 / 8, HashBytes = 16 };
|
||||
|
||||
/// same as reset()
|
||||
MD5();
|
||||
|
||||
/// compute MD5 of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute MD5 of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as 32 hex characters
|
||||
std::string getHash();
|
||||
/// return latest hash as bytes
|
||||
void getHash(unsigned char buffer[HashBytes]);
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// process 64 bytes
|
||||
void processBlock(const void* data);
|
||||
/// process everything left in the internal buffer
|
||||
void processBuffer();
|
||||
|
||||
/// size of processed data in bytes
|
||||
uint64_t m_numBytes;
|
||||
/// valid bytes in m_buffer
|
||||
size_t m_bufferSize;
|
||||
/// bytes not processed yet
|
||||
uint8_t m_buffer[BlockSize];
|
||||
|
||||
enum { HashValues = HashBytes / 4 };
|
||||
/// hash, stored as integers
|
||||
uint32_t m_hash[HashValues];
|
||||
};
|
78
module/Vendor/Hash/include/sha1.h
vendored
Normal file
78
module/Vendor/Hash/include/sha1.h
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// sha1.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute SHA1 hash
|
||||
/** Usage:
|
||||
SHA1 sha1;
|
||||
std::string myHash = sha1("Hello World"); // std::string
|
||||
std::string myHash2 = sha1("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
SHA1 sha1;
|
||||
while (more data available)
|
||||
sha1.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = sha1.getHash();
|
||||
*/
|
||||
class SHA1 //: public Hash
|
||||
{
|
||||
public:
|
||||
/// split into 64 byte blocks (=> 512 bits), hash is 20 bytes long
|
||||
enum { BlockSize = 512 / 8, HashBytes = 20 };
|
||||
|
||||
/// same as reset()
|
||||
SHA1();
|
||||
|
||||
/// compute SHA1 of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute SHA1 of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as 40 hex characters
|
||||
std::string getHash();
|
||||
/// return latest hash as bytes
|
||||
void getHash(unsigned char buffer[HashBytes]);
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// process 64 bytes
|
||||
void processBlock(const void* data);
|
||||
/// process everything left in the internal buffer
|
||||
void processBuffer();
|
||||
|
||||
/// size of processed data in bytes
|
||||
uint64_t m_numBytes;
|
||||
/// valid bytes in m_buffer
|
||||
size_t m_bufferSize;
|
||||
/// bytes not processed yet
|
||||
uint8_t m_buffer[BlockSize];
|
||||
|
||||
enum { HashValues = HashBytes / 4 };
|
||||
/// hash, stored as integers
|
||||
uint32_t m_hash[HashValues];
|
||||
};
|
78
module/Vendor/Hash/include/sha256.h
vendored
Normal file
78
module/Vendor/Hash/include/sha256.h
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// sha256.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute SHA256 hash
|
||||
/** Usage:
|
||||
SHA256 sha256;
|
||||
std::string myHash = sha256("Hello World"); // std::string
|
||||
std::string myHash2 = sha256("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
SHA256 sha256;
|
||||
while (more data available)
|
||||
sha256.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = sha256.getHash();
|
||||
*/
|
||||
class SHA256 //: public Hash
|
||||
{
|
||||
public:
|
||||
/// split into 64 byte blocks (=> 512 bits), hash is 32 bytes long
|
||||
enum { BlockSize = 512 / 8, HashBytes = 32 };
|
||||
|
||||
/// same as reset()
|
||||
SHA256();
|
||||
|
||||
/// compute SHA256 of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute SHA256 of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as 64 hex characters
|
||||
std::string getHash();
|
||||
/// return latest hash as bytes
|
||||
void getHash(unsigned char buffer[HashBytes]);
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// process 64 bytes
|
||||
void processBlock(const void* data);
|
||||
/// process everything left in the internal buffer
|
||||
void processBuffer();
|
||||
|
||||
/// size of processed data in bytes
|
||||
uint64_t m_numBytes;
|
||||
/// valid bytes in m_buffer
|
||||
size_t m_bufferSize;
|
||||
/// bytes not processed yet
|
||||
uint8_t m_buffer[BlockSize];
|
||||
|
||||
enum { HashValues = HashBytes / 4 };
|
||||
/// hash, stored as integers
|
||||
uint32_t m_hash[HashValues];
|
||||
};
|
81
module/Vendor/Hash/include/sha3.h
vendored
Normal file
81
module/Vendor/Hash/include/sha3.h
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
// //////////////////////////////////////////////////////////
|
||||
// sha3.h
|
||||
// Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
|
||||
// see http://create.stephan-brumme.com/disclaimer.html
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include "hash.h"
|
||||
#include <string>
|
||||
|
||||
// define fixed size integer types
|
||||
#ifdef _MSC_VER
|
||||
// Windows
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
// GCC
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
|
||||
/// compute SHA3 hash
|
||||
/** Usage:
|
||||
SHA3 sha3;
|
||||
std::string myHash = sha3("Hello World"); // std::string
|
||||
std::string myHash2 = sha3("How are you", 11); // arbitrary data, 11 bytes
|
||||
|
||||
// or in a streaming fashion:
|
||||
|
||||
SHA3 sha3;
|
||||
while (more data available)
|
||||
sha3.add(pointer to fresh data, number of new bytes);
|
||||
std::string myHash3 = sha3.getHash();
|
||||
*/
|
||||
class SHA3 //: public Hash
|
||||
{
|
||||
public:
|
||||
/// algorithm variants
|
||||
enum Bits { Bits224 = 224, Bits256 = 256, Bits384 = 384, Bits512 = 512 };
|
||||
|
||||
/// same as reset()
|
||||
explicit SHA3(Bits bits = Bits256);
|
||||
|
||||
/// compute hash of a memory block
|
||||
std::string operator()(const void* data, size_t numBytes);
|
||||
/// compute hash of a string, excluding final zero
|
||||
std::string operator()(const std::string& text);
|
||||
|
||||
/// add arbitrary number of bytes
|
||||
void add(const void* data, size_t numBytes);
|
||||
|
||||
/// return latest hash as hex characters
|
||||
std::string getHash();
|
||||
|
||||
/// restart
|
||||
void reset();
|
||||
|
||||
private:
|
||||
/// process a full block
|
||||
void processBlock(const void* data);
|
||||
/// process everything left in the internal buffer
|
||||
void processBuffer();
|
||||
|
||||
/// 1600 bits, stored as 25x64 bit, BlockSize is no more than 1152 bits (Keccak224)
|
||||
enum { StateSize = 1600 / (8 * 8),
|
||||
MaxBlockSize = 200 - 2 * (224 / 8) };
|
||||
|
||||
/// hash
|
||||
uint64_t m_hash[StateSize];
|
||||
/// size of processed data in bytes
|
||||
uint64_t m_numBytes;
|
||||
/// block size (less or equal to MaxBlockSize)
|
||||
size_t m_blockSize;
|
||||
/// valid bytes in m_buffer
|
||||
size_t m_bufferSize;
|
||||
/// bytes not processed yet
|
||||
uint8_t m_buffer[MaxBlockSize];
|
||||
/// variant
|
||||
Bits m_bits;
|
||||
};
|
Reference in New Issue
Block a user