summaryrefslogtreecommitdiffstats
blob: 0cea08f2349dc4cd132090c79563450d33770ace (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swiften/StringCodecs/HMACSHA1.h"

#include <cassert>

#include "Swiften/StringCodecs/SHA1.h"
#include "Swiften/Base/ByteArray.h"

namespace Swift {

static const unsigned int B = 64;

ByteArray HMACSHA1::getResult(const ByteArray& key, const ByteArray& data) {
	assert(key.getSize() <= B);

	// Create the padded key
	ByteArray paddedKey(key);
	paddedKey.resize(B, 0x0);

	// Create the first value
	ByteArray x(paddedKey);
	for (unsigned int i = 0; i < x.getSize(); ++i) {
		x[i] ^= 0x36;
	}
	x += data;

	// Create the second value
	ByteArray y(paddedKey);
	for (unsigned int i = 0; i < y.getSize(); ++i) {
		y[i] ^= 0x5c;
	}
	y += SHA1::getHash(x);

	return SHA1::getHash(y);
}

}