summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/BHashes.h90
-rw-r--r--includes/BSHA1.h21
-rw-r--r--src/BWebSocket.cc2
-rw-r--r--win32/project/Balau.vcxproj2
-rw-r--r--win32/project/Balau.vcxproj.filters6
5 files changed, 95 insertions, 26 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;
-};
-
-};
diff --git a/src/BWebSocket.cc b/src/BWebSocket.cc
index 7d6bf1c..7cd97cd 100644
--- a/src/BWebSocket.cc
+++ b/src/BWebSocket.cc
@@ -1,5 +1,5 @@
#include "BWebSocket.h"
-#include "BSHA1.h"
+#include "BHashes.h"
#include "Base64.h"
#include "TaskMan.h"
diff --git a/win32/project/Balau.vcxproj b/win32/project/Balau.vcxproj
index 07d6277..5cc49d5 100644
--- a/win32/project/Balau.vcxproj
+++ b/win32/project/Balau.vcxproj
@@ -267,10 +267,10 @@
<ClInclude Include="..\..\includes\Async.h" />
<ClInclude Include="..\..\includes\AtStartExit.h" />
<ClInclude Include="..\..\includes\Base64.h" />
+ <ClInclude Include="..\..\includes\BHashes.h" />
<ClInclude Include="..\..\includes\BigInt.h" />
<ClInclude Include="..\..\includes\BLua.h" />
<ClInclude Include="..\..\includes\BRegex.h" />
- <ClInclude Include="..\..\includes\BSHA1.h" />
<ClInclude Include="..\..\includes\BStdIO.h" />
<ClInclude Include="..\..\includes\BStream.h" />
<ClInclude Include="..\..\includes\BString.h" />
diff --git a/win32/project/Balau.vcxproj.filters b/win32/project/Balau.vcxproj.filters
index c9b12a4..98b3ffd 100644
--- a/win32/project/Balau.vcxproj.filters
+++ b/win32/project/Balau.vcxproj.filters
@@ -194,9 +194,6 @@
<ClInclude Include="..\..\includes\BRegex.h">
<Filter>Headers</Filter>
</ClInclude>
- <ClInclude Include="..\..\includes\BSHA1.h">
- <Filter>Headers</Filter>
- </ClInclude>
<ClInclude Include="..\..\includes\BStdIO.h">
<Filter>Headers</Filter>
</ClInclude>
@@ -335,6 +332,9 @@
<ClInclude Include="..\..\includes\CurlTask.h">
<Filter>Headers</Filter>
</ClInclude>
+ <ClInclude Include="..\..\includes\BHashes.h">
+ <Filter>Headers</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\jsoncpp\src\json_internalarray.inl">