金鑰雜湊訊息鑑別碼
維基百科,自由的百科全書
|
|
本條目可參照英語維基百科相應條目來擴充。 |
金鑰雜湊訊息鑑別碼(英語:Keyed-hash message authentication code,縮寫為HMAC),又稱雜湊訊息鑑別碼(Hash-based message authentication code),是一種通過特別計算方式之後產生的訊息鑑別碼(MAC),使用密碼雜湊函數,同時結合一個加密金鑰。它可以用來保證數據的完整性,同時可以用來作某個訊息的身份驗證。
定義[編輯]
根據RFC 2104,HMAC的數學公式為:
其中:
- H為密碼雜湊函數(如MD5或SHA-1)
- K為密鑰(secret key)
- m是要認證的訊息
- K'是從原始金鑰K匯出的另一個秘密金鑰(如果K短於雜湊函數的輸入塊大小,則向右填充(Padding)零;如果比該塊大小更長,則對K進行雜湊)
- || 代表串接
- ⊕ 代表異或(XOR)
- opad 是外部填充(0x5c5c5c…5c5c,一段十六進制常量)
- ipad 是內部填充(0x363636…3636,一段十六進制常量)
實現[編輯]
下面的偽代碼展示了如何實現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
}
相關條目[編輯]
參考文獻[編輯]
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||