Ger*_*ann 6 c++ hash qt sha1 hmac
我正在尝试在我的C++/Qt应用程序中实现HMAC-SHA1算法.我有一个Sha1算法可用的方法,我只需要了解它的HMAC部分.
这个伪代码来自维基百科:
1 function hmac (key, message)
2 if (length(key) > blocksize) then
3 // keys longer than blocksize are shortened
4 key = hash(key)
5 end if
6 if (length(key) < blocksize) then
7 // keys shorter than blocksize are zero-padded
8 key = key ? zeroes(blocksize - length(key))
9 end if
10
11 // Where blocksize is that of the underlying hash function
12 o_key_pad = [0x5c * blocksize] ? key
13 i_key_pad = [0x36 * blocksize] ? key // Where ? is exclusive or (XOR)
14 // Where ? is concatenation
15 return hash(o_key_pad ? hash(i_key_pad ? message))
16 end function
Run Code Online (Sandbox Code Playgroud)
什么是块大小?零线函数在第8行上做了什么?你如何在C++中表达第12-13行?
通常,散列算法通过将数据切割成固定大小数据块(也称为"块")来处理数据.对于SHA1,我通常的块大小是64字节.
它(如注释所述)将"零"添加到键的末尾,以使其长度与"块"大小匹配.
我想你正在寻找XOR运算符:^
.
例:
o_key_pad = (0x5c * blocksize) ^ key; // Actually, it should be 0x5c5c5c... repeated enough so that it matches key size.
Run Code Online (Sandbox Code Playgroud)
快速说明:这没什么特别的Qt
,您可能希望在"原始"中执行此操作,C++
以便最终可以在非Qt
项目中重复使用它.Qt
是伟大的imho,但你显然不需要它来实现这一点.
这篇文章有一个有效的实现:
/**
* Hashes the given string using the HMAC-SHA1 algorithm.
*
* \param key The string to be hashed
* \param secret The string that contains secret word
* \return The hashed string
*/
static QString hmac_sha1(const QString &key, const QString &secret) {
// Length of the text to be hashed
int text_length;
// For secret word
QByteArray K;
// Length of secret word
int K_length;
K_length = secret.size();
text_length = key.size();
// Need to do for XOR operation. Transforms QString to
// unsigned char
K = secret.toAscii();
// Inner padding
QByteArray ipad;
// Outer padding
QByteArray opad;
// If secret key > 64 bytes use this to obtain sha1 key
if (K_length > 64) {
QByteArray tempSecret;
tempSecret.append(secret);
K = QCryptographicHash::hash(tempSecret, QCryptographicHash::Sha1);
K_length = 20;
}
// Fills ipad and opad with zeros
ipad.fill(0, 64);
opad.fill(0, 64);
// Copies Secret to ipad and opad
ipad.replace(0, K_length, K);
opad.replace(0, K_length, K);
// XOR operation for inner and outer pad
for (int i = 0; i < 64; i++) {
ipad[i] = ipad[i] ^ 0x36;
opad[i] = opad[i] ^ 0x5c;
}
// Stores hashed content
QByteArray context;
// Appends XOR:ed ipad to context
context.append(ipad, 64);
// Appends key to context
context.append(key);
//Hashes inner pad
QByteArray Sha1 = QCryptographicHash::hash(context, QCryptographicHash::Sha1);
context.clear();
//Appends opad to context
context.append(opad, 64);
//Appends hashed inner pad to context
context.append(Sha1);
Sha1.clear();
// Hashes outerpad
Sha1 = QCryptographicHash::hash(context, QCryptographicHash::Sha1);
// String to return hashed stuff in Base64 format
QByteArray str(Sha1.toBase64());
return str;
}
Run Code Online (Sandbox Code Playgroud)