diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 23:32:32 -0700 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 23:32:32 -0700 |
commit | 676852ca9ba566c75f4e5aa5f3853ca20ae88189 (patch) | |
tree | af2c7d675a0e3447b2b7ef6982a4e3be2740fefb /includes | |
parent | df9b8f8ae7a540988c20fdffaaff2442c349873a (diff) |
Better hashes implementation.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/BHashes.h | 90 | ||||
-rw-r--r-- | includes/BSHA1.h | 21 |
2 files changed, 90 insertions, 21 deletions
diff --git a/includes/BHashes.h b/includes/BHashes.h new file mode 100644 index 0000000..505e417 --- /dev/null +++ b/includes/BHashes.h @@ -0,0 +1,90 @@ +#pragma once + +#include <array> +#include <Exceptions.h> +#include <tomcrypt.h> + +namespace Balau { + +template<const struct ltc_hash_descriptor * desc> +class Hash { + public: + Hash() { reset(); } + void reset() { + int r = desc->init(&m_state); + IAssert(r == CRYPT_OK, "init for %s returned %i", desc->name, r); + } + void update(const uint8_t * data, const size_t len) { + int r = desc->process(&m_state, data, len); + IAssert(r == CRYPT_OK, "process for %s returned %i", desc->name, r); + } + void update(const String & data) { update((uint8_t *) data.to_charp(), data.strlen()); } + template<size_t L> + void update(const char (&str)[L]) { update((uint8_t *) str, L - 1); } + template<size_t L> + unsigned final(uint8_t (&digest)[L]) { return final(digest, L); } + unsigned final(void * digest, unsigned outlen) { + AAssert(outlen >= digestSize(), "digest size too small being passed on for %s: %u instead of %u", name(), outlen, digestSize()); + int r = desc->done(&m_state, (uint8_t *)digest); + IAssert(r == CRYPT_OK, "done for %s returned %i", desc->name, r); + return digestSize(); + } + + static const unsigned digestSize() { return desc->hashsize; } + static const unsigned blockSize() { return desc->blocksize; } + static const char * name() { return desc->name; } + static char hashID() { return desc->ID; } + + private: + hash_state m_state; +}; + +template<class Hash> +class HMAC { + public: + void prepare(const uint8_t * key, const size_t len) { + int r = hmac_init(&m_state, find_hash_id(Hash::hashID()), key, len); + IAssert(r == CRYPT_OK, "hmac_init for %s returned %i", Hash::name(), r); + } + void prepare(const String & data) { prepare((uint8_t *) data.to_charp(), data.strlen()); } + template<size_t L> + void prepare(const char (&str)[L]) { prepare((uint8_t *) str, L - 1); } + void update(const uint8_t * data, const size_t len) { + int r = hmac_process(&m_state, data, len); + IAssert(r == CRYPT_OK, "hmac_process for %s returned %i", Hash::name(), r); + } + void update(const String & data) { update((uint8_t *) data.to_charp(), data.strlen()); } + template<size_t L> + void update(const char (&str)[L]) { update((uint8_t *) str, L - 1); } + template<size_t L> + unsigned final(uint8_t(&digest)[L]) { return final(digest, L); } + unsigned final(void * digest, unsigned outlen) { + int r = hmac_done(&m_state, (uint8_t *) digest, &outlen); + IAssert(r == CRYPT_OK, "hmac_done for %s returned %i", Hash::name(), r); + return outlen; + } + + static const unsigned digestSize() { return Hash::digestSize(); } + static const unsigned blockSize() { return Hash::blockSize(); } + + private: + hmac_state m_state; +}; + +class CHC : public Hash<&chc_desc> { };
+class Whirlpool : public Hash<&whirlpool_desc> { };
+class SHA512 : public Hash<&sha512_desc> { };
+class SHA384 : public Hash<&sha384_desc> { };
+class SHA256 : public Hash<&sha256_desc> { };
+class SHA224 : public Hash<&sha224_desc> { };
+class SHA1 : public Hash<&sha1_desc> { };
+class MD5 : public Hash<&md5_desc> { };
+class MD4 : public Hash<&md4_desc> { };
+class MD2 : public Hash<&md2_desc> { };
+class Tiger : public Hash<&tiger_desc> { };
+class RMD128 : public Hash<&rmd128_desc> { };
+class RMD160 : public Hash<&rmd160_desc> { };
+class RMD256 : public Hash<&rmd256_desc> { };
+class RMD320 : public Hash<&rmd320_desc> { };
+ +}; diff --git a/includes/BSHA1.h b/includes/BSHA1.h deleted file mode 100644 index b58a890..0000000 --- a/includes/BSHA1.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include <Exceptions.h> -#include <tomcrypt.h> - -namespace Balau { - -class SHA1 { - public: - SHA1() { reset(); } - void reset() { sha1_init(&m_state); } - void update(const uint8_t * data, const size_t len) { sha1_process(&m_state, data, len); } - void final(uint8_t * digest) { sha1_done(&m_state, digest); } - - enum { DIGEST_SIZE = 20 }; - - private: - hash_state m_state; -}; - -}; |