如何在Qt中实现HMAC-SHA1算法

时间:2010-07-27 06:20:04

标签: c++ qt hash 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

什么是块大小?零线函数在第8行上做了什么?你如何在C ++中表达第12-13行?

5 个答案:

答案 0 :(得分:6)

1.什么是块大小?

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

2。第8行的零功能是什么?

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

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非常棒,但你显然不需要它来实现它。

答案 1 :(得分:5)

This post有一个有效的实施方案:

/**
 * 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;
}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

从Qt 5.1开始,QMessageAuthenticationCode会根据您选择的QCryptographicHash::Algorithm生成HMAC。