【发布时间】:2011-03-21 10:46:00
【问题描述】:
我正在尝试在我的 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
什么是块大小? zeroes 函数在第 8 行有什么作用?如何用 C++ 表达第 12-13 行?
【问题讨论】: