我正在尝试在我的C / Qt应用程序中实现HMAC-SHA1算法.
我有一个Sha1算法可用的方法,我只需要了解它的HMAC部分.
我有一个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,但你显然不需要它来实现这一点.
原文地址:https://www.jb51.cc/c/115762.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。