【发布时间】:2017-05-09 11:37:18
【问题描述】:
我必须实现自己的 HMAC-SHA256 才能在嵌入式项目中使用。我无法让它工作。我什至无法获得手动计算的伪代码,所以我知道我做错了什么!
我的伪代码计算。按照维基百科中的图表
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
当我手动计算 key="mykey" 和 message="helloworld" 时,我得到以下信息:
key = 0x6d796b6579000000000000000000000000000000000000000000000000000000
o_key_pad = 0x31253739255c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c
i_key_pad = 0x5b4f5d534f363636363636363636363636363636363636363636363636363636
hash(i_key_pad ∥ message) = 6fb2e91de7b8b5ec6283846ff7245cd6eb4a4fd26056b529bd42d99fcf3314d2
和0d76a16089f85cd2169bb64b6f2c818e6a404a218896483fcd97fee5cce185ae的整体hmac
【问题讨论】:
标签: cryptography pseudocode sha256 hmac