HMAC (有时扩展为 英语:keyed-hash message authentication code, 密钥散列消息认证码, 或 英语:hash-based message authentication code,散列消息认证码),是一种通过特别计算方式之后产生的消息认证码(MAC),使用密码散列函数,同时结合一个加密密钥。它可以用来保证资料的完整性,同时可以用来作某个消息的身份验证。
此条目可参照英语维基百科相应条目来扩充。 |
定义
根据RFC 2104,HMAC的数学公式为:
其中:
实现
下面的伪代码展示了如何实现HMAC。当使用以下散列函数之一时,块大小为64(字节):SHA-1、MD5、RIPEMD-128/160[1]。
function hmac (key, message) { if (length(key) > blocksize) { key = hash(key) // keys longer than blocksize are shortened } if (length(key) < blocksize) { // keys shorter than blocksize are zero-padded (where ∥ is concatenation) key = key ∥ [0x00 * (blocksize - length(key))] // Where * is repetition. } o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR) return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where ∥ is concatenation }
相关条目
参考文献
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.