Add sources

This commit is contained in:
2018-06-05 22:08:10 +02:00
parent bcc80a4727
commit c8947f3bac
17 changed files with 802 additions and 0 deletions

187
test/Hash.h Normal file
View File

@@ -0,0 +1,187 @@
#pragma once
#include "base_types.h"
extern "C"
{
#include "md5.h"
#include "sha1.h"
#include "sha256.h"
}
template<unsigned int HASH_SIZE>
struct HashNull
{
enum { SIZE = HASH_SIZE };
HashNull(void)
{
setBytes(0);
}
bool initFromData(const byte * data, unsigned int size)
{
setBytes(0);
return(true);
}
HashNull<SIZE> & operator = (byte data)
{
setBytes(data);
}
byte operator [] (unsigned int index) const
{
return(checksum[index]);
}
protected:
void setBytes(byte data)
{
for (int i = 0; i < SIZE; ++i)
{
checksum[i] = data;
}
}
private:
byte checksum [SIZE];
};
struct HashMd5
{
enum { SIZE = 16 }; // 128 bits
HashMd5(void)
{
setBytes(0);
}
bool initFromData(byte * data, unsigned int size)
{
MD5_CTX context;
md5_init(&context);
md5_update(&context, data, size);
md5_final(&context, checksum);
return(true);
}
HashMd5 & operator = (byte data)
{
setBytes(data);
}
byte operator [] (unsigned int index) const
{
return(checksum[index]);
}
protected:
void setBytes(byte data)
{
for (int i = 0; i < SIZE; ++i)
{
checksum[i] = data;
}
}
private:
byte checksum [SIZE];
};
struct HashSha1
{
enum { SIZE = 20 }; // 160 bits
HashSha1(void)
{
setBytes(0);
}
bool initFromData(byte * data, unsigned int size)
{
SHA1_CTX context;
sha1_init(&context);
sha1_update(&context, data, size);
sha1_final(&context, checksum);
return(true);
}
HashSha1 & operator = (byte data)
{
setBytes(data);
}
byte operator [] (unsigned int index) const
{
return(checksum[index]);
}
protected:
void setBytes(byte data)
{
for (int i = 0; i < SIZE; ++i)
{
checksum[i] = data;
}
}
private:
byte checksum [SIZE];
};
struct HashSha256
{
enum { SIZE = 32 }; // 256 bits
HashSha256(void)
{
setBytes(0);
}
bool initFromData(byte * data, unsigned int size)
{
SHA256_CTX context;
sha256_init(&context);
sha256_update(&context, data, size);
sha256_final(&context, checksum);
return(true);
}
HashSha256 & operator = (byte data)
{
setBytes(data);
}
byte operator [] (unsigned int index) const
{
return(checksum[index]);
}
protected:
void setBytes(byte data)
{
for (int i = 0; i < SIZE; ++i)
{
checksum[i] = data;
}
}
private:
byte checksum [SIZE];
};