如何在Qt中实现HMAC-SHA1算法
我正在尝试在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
1. 区块大小是多少?
通常,哈希算法通过将数据切割成固定大小的数据块(也称为“块”)来处理数据。对于 SHA1,我通常的块大小是 64 字节。
2. 第 8 行的 Zeros 函数有什么作用?
它(如注释所述)在密钥末尾添加“零”,以便其长度与“块”大小匹配。
3.如何用C++表达第12-13行?
我认为您正在寻找 XOR 运算符:
^
。示例:
简单说明:这与
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:
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.这篇文章有一个可行的实现:
This post has a working implementation:
从 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.
查看 QCA 库。它已经提供了所有主要加密算法的实现。
Take at look at the QCA library. It already provides implementations of all major cryptographic algorithms.
您还应该看看 QCryptographicHash,因为它可以帮助您sha1 是你问题的一部分。
You should also take a look at QCryptographicHash, since it can help you with the sha1 part of your problem.