HMAC (有时扩展为 英語:keyed-hash message authentication code, 金鑰雜湊訊息鑑別碼, 或 英語:hash-based message authentication code雜湊訊息鑑別碼),是一種通過特別計算方式之後產生的訊息鑑別碼(MAC),使用密碼雜湊函數,同時結合一個加密金鑰。它可以用來保證資料的完整性,同時可以用來作某個訊息的身份驗證

SHA-1 HMAC產生過程

定義

编辑

根據RFC 2104,HMAC的數學公式為:  

其中:

H為密碼雜湊函數(如SHA家族
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
}

相關條目

编辑

参考文献

编辑
  1. ^ RFC 2104, section 2, "Definition of HMAC", page 3.

📚 Artikel Terkait di Wikipedia

MD5

MD5訊息摘要演算法(英語:MD5 Message-Digest Algorithm),一種被廣泛使用的密碼雜湊函數,可以產生出一個128位元(16個字节)的散列值(hash value),用于确保信息传输完整一致。MD5由美國密碼學家罗纳德·李维斯特(Ronald Linn

信息鉴定

信息鉴定(Message authentication),又稱数据原发鉴别,是信息安全中指一项信息在传输过程中未受更改(数据完整性)且接收方能验证信息来源的性质。信息鉴定不需要不可否认(Non-repudiation)性质。 信息鉴定通常通过使用訊息鑑別碼、认证加密或數位簽章方式完成。

认证加密

encryption with associated data,AEAD,AE的变种)是一种能够同时保证数据的保密性、 完整性和真实性(英语:message authentication)的一种加密模式。这些属性都是在一个易于使用的编程接口下提供的。 人们观察发现安全地将保密模式与认证模式组合可能是容易出错和困难的,于是认证加密应运而生。

Google身份验证器

SailOTP Apache: Google Authenticator Apache Module PAM: Google Pluggable Authentication Module oauth-pam Backend: LinOTP(英语:LinOTP) (后端管理使用Python实现) Chrome/Chrome

BLAKE

Saarinen, M-J; Aumasson, J-P. The BLAKE2 Cryptographic Hash and Message Authentication Code (MAC). IETF. November 2015 [4 December 2015]. RFC 7693.  BLAKE2

SHA家族

AMD Opteron 8354 2.2 GHz processor running 64-bit Linux The MD5 Message-Digest Algorithm. [2016-04-18]. (原始内容存档于2017-04-26).  The SHAppening: freestart

SHA-1

SHA-1(英語:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数,美国国家安全局设计,并由美国國家標準技術研究所(NIST)发布为聯邦資料處理標準(FIPS)。SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。

双棘轮算法

Ratchet Algorithm. Signal. [2016-11-20]. (原始内容存档于2017-07-08).  H. Krawczyk, M. Bellare, and R. Canetti, “HMAC: Keyed-Hashing for Message Authentication.” Internet