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

发布于 2024-09-11 05:05:02 字数 826 浏览 7 评论 0原文

我正在尝试在我的 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 行的 Zeroes 函数有什么作用?如何用 C++ 表达第 12-13 行?

I'm trying to implement HMAC-SHA1 algorithm in my C++/Qt application.
I have a method for Sha1 algorithm available, I just need to understand the HMAC part of it.

This pseudocode is from wikipedia:

 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

What is the blocksize? What does the zeroes-function do on line 8? How do you express lines 12-13 in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话里做英雄 2024-09-18 05:05:02

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 很棒,但您显然不需要它来实现这一点。

1. What is the blocksize ?

Usually, hash algorithm process data by cutting it into chunks of fixed size data (aka. "blocks"). For SHA1, I the usual block size is 64 bytes.

2. What does the zeros function do on line 8 ?

It (as the comment states) adds "zeroes" to the end of key so that its length matches the "block" size.

3. How do you express lines 12-13 in C++ ?

I think you're looking for the XOR operator: ^.

Example:

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

Just a quick note: this has nothing special to do with Qt and you will probably want to do it in "raw" C++ so that you can eventually reuse it in a non-Qt project. Qt is great imho, but you clearly don't require it to implement this.

羅雙樹 2024-09-18 05:05:02

这篇文章有一个可行的实现:

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

This post has a working implementation:

/**
 * 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;
}
[旋木] 2024-09-18 05:05:02

从 Qt 5.1 开始,有 QMessageAuthenticationCode ,它将使用 QCryptographicHash::Algorithm 您选择的。

Since Qt 5.1 there is the QMessageAuthenticationCode that will generate HMAC with the QCryptographicHash::Algorithm of your choice.

淡紫姑娘! 2024-09-18 05:05:02

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

Take at look at the QCA library. It already provides implementations of all major cryptographic algorithms.

贵在坚持 2024-09-18 05:05:02

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

You should also take a look at QCryptographicHash, since it can help you with the sha1 part of your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文