6

我正在尝试在我的 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

什么是块大小?zeroes 函数在第 8 行有什么作用?你如何用 C++ 表达第 12-13 行?

4

5 回答 5

6

1. 什么是块大小?

通常,哈希算法通过将数据切割成固定大小的数据块(又名“块”)来处理数据。对于 SHA1,我通常的块大小是 64 字节。

2. 第 8 行的 zeros 函数有什么作用?

它(如评论所述)在键的末尾添加“零”,使其长度与“块”大小匹配。

3. 你如何用 C++ 表达第 12-13 行?

我认为您正在寻找 XOR 运算符:^

例子:

o_key_pad = (0x5c * blocksize) ^ key; // Actually, it should be 0x5c5c5c... repeated enough so that it matches key size.

只是一个简短的说明:这没有什么特别的关系Qt,您可能希望在“原始”中执行它,C++以便您最终可以在非Qt项目中重用它。Qt是伟大的恕我直言,但你显然不需要它来实现这一点。

于 2010-07-27T06:44:06.647 回答
5

这篇文章有一个有效的实现:

/**
 * 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;
}
于 2010-08-29T08:25:10.573 回答
2

从 Qt 5.1 开始,QMessageAuthenticationCode将使用您选择的QCryptographicHash::Algorithm生成 HMAC。

于 2019-01-15T14:36:52.823 回答
1

看看QCA库。它已经提供了所有主要加密算法的实现。

于 2010-07-27T07:28:59.993 回答
1

您还应该看看QCryptographicHash,因为它可以帮助您解决问题的 sha1 部分。

于 2010-07-27T09:05:32.930 回答