blob: 438a3a743fe17dca4614bd653ccf0a14ce52a962 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
}
};
}
|