#pragma once #include "base_types.h" extern "C" { #include "md5.h" #include "sha1.h" #include "sha256.h" } template struct HashNull { enum { SIZE = HASH_SIZE }; HashNull(void) { setBytes(0); } bool initFromData(const byte * data, unsigned int size) { setBytes(0); return(true); } HashNull & 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]; };