Repository navigation
Expand file tree
/
Copy pathtcryptmac.cpp
More file actions
69 lines (60 loc) · 1.99 KB
/
Copy pathtcryptmac.cpp
File metadata and controls
69 lines (60 loc) · 1.99 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright (c) 2011-2015, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <QCryptographicHash>
#include <QHash>
#include <QScopedPointer> // fix compilation error in Qt5.0
#if QT_VERSION >= 0x050100
# include <QMessageAuthenticationCode>
#endif
#include <TCryptMac>
#if QT_VERSION < 0x050100
class BlockSizeHash : public QHash<int, int>
{
public:
BlockSizeHash() : QHash<int, int>()
{
insert(TCryptMac::Hmac_Md5, 64);
insert(TCryptMac::Hmac_Sha1, 64);
#if QT_VERSION >= 0x050000
insert(TCryptMac::Hmac_Sha256, 64);
insert(TCryptMac::Hmac_Sha384, 128);
insert(TCryptMac::Hmac_Sha512, 128);
#endif
}
};
Q_GLOBAL_STATIC(BlockSizeHash, blockSizeHash)
#endif
/*!
\class TCryptMac
\brief The TCryptMac class provides the functionality of a
"Message Authentication Code" (MAC) algorithm.
*/
/*!
Returns a cryptographic hash value generated from the given binary or
text data \a data with \a key using \a method.
*/
QByteArray TCryptMac::hash(const QByteArray &data, const QByteArray &key, Algorithm method)
{
#if QT_VERSION < 0x050100
int blockSize = blockSizeHash()->value(method);
QByteArray tk = (key.length() > blockSize) ? QCryptographicHash::hash(key, (QCryptographicHash::Algorithm)method) : key;
QByteArray k_ipad(blockSize, '\0');
k_ipad.replace(0, tk.length(), tk);
QByteArray k_opad = k_ipad;
// XOR key with ipad and opad values
for (int i = 0; i < blockSize; ++i) {
k_ipad[i] = k_ipad[i] ^ 0x36;
k_opad[i] = k_opad[i] ^ 0x5c;
}
k_ipad.append(data);
QByteArray hash = QCryptographicHash::hash(k_ipad, (QCryptographicHash::Algorithm)method);
k_opad.append(hash);
return QCryptographicHash::hash(k_opad, (QCryptographicHash::Algorithm)method);
#else
return QMessageAuthenticationCode::hash(data, key, (QCryptographicHash::Algorithm)method);
#endif
}