summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-08-12 17:29:21 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-08-12 17:32:17 (GMT)
commitc6ffcd27e94d2f90fd4a3bcb5d2d3c6550ead59c (patch)
tree5968399a0d1ae73c1f197630bef3a0960e8525b5 /Swiften/StringCodecs/HMAC.h
parent08cfaa06859238449d6848df4e170ffb6dc605d3 (diff)
downloadswift-c6ffcd27e94d2f90fd4a3bcb5d2d3c6550ead59c.zip
swift-c6ffcd27e94d2f90fd4a3bcb5d2d3c6550ead59c.tar.bz2
Refactored stringcodec functions to make them independent of hash algos.
Diffstat (limited to 'Swiften/StringCodecs/HMAC.h')
-rw-r--r--Swiften/StringCodecs/HMAC.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/Swiften/StringCodecs/HMAC.h b/Swiften/StringCodecs/HMAC.h
new file mode 100644
index 0000000..438a3a7
--- /dev/null
+++ b/Swiften/StringCodecs/HMAC.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/ByteArray.h>
+#include <Swiften/Base/Algorithm.h>
+#include <cassert>
+
+namespace Swift {
+ namespace HMAC_Detail {
+ static const unsigned int B = 64;
+
+ template<typename Hash, typename KeyType>
+ static ByteArray getHMAC(const KeyType& key, const ByteArray& data) {
+ assert(key.size() <= B);
+ Hash hash;
+
+ // Create the padded key
+ KeyType paddedKey(key);
+ paddedKey.resize(B, 0x0);
+
+ // Create the first value
+ KeyType x(paddedKey);
+ for (unsigned int i = 0; i < x.size(); ++i) {
+ x[i] ^= 0x36;
+ }
+ append(x, data);
+
+ // Create the second value
+ KeyType y(paddedKey);
+ for (unsigned int i = 0; i < y.size(); ++i) {
+ y[i] ^= 0x5c;
+ }
+ append(y, hash(x));
+
+ return hash(y);
+ }
+ };
+
+ template<typename Hash>
+ class HMAC {
+ private:
+
+ public:
+ ByteArray operator()(const ByteArray& key, const ByteArray& data) const {
+ return HMAC_Detail::getHMAC<Hash,ByteArray>(key, data);
+ }
+
+ ByteArray operator()(const SafeByteArray& key, const ByteArray& data) const {
+ return HMAC_Detail::getHMAC<Hash,SafeByteArray>(key, data);
+ }
+ };
+}