c – 如何在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行上做了什么?你如何表达C中的12-13行?

解决方法

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是伟大的imho,但你显然不需要它来实现这一点.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...