HMAC
HMAC(Hash-based Message Authentication Code)
HMAC(key, msg, hash_func) -> hash
定義
$ HMAC(K, text) = H((K_0 \oplus opad) || H((K_0 \oplus ipad) || text)))
$ H : 繰り返し型ハッシュ関数
$ K : 秘密鍵
$ K_0 : Bバイトのキーを形成するために必要な前処理を行った後のキー$ K (?)
Bはハッシュ関数のブロックサイズ
$ opad : 定数パディング
バイト値 0x5c を B回繰り返した文字列
$ ipad : 定数パディング
バイト値 0x36を B回繰り返した文字列
$ text : 認証対象のメッセージ
$ || は結合処理
9ステップからなる
$ L : ハッシュ出力用のバイト長
code:memo
if length(K) = B {
K_0 = K
} else if length(K) > B {
-- ?
K_0 = H(K) ++ 00..00
} else if length(K) < B {
-- rpadはPL/SQLのrpadのイメージ
K_0 = rpad(K, B, 0x00)
}
-- take, repeatはHaskellの関数のイメージ
K0_ipad_text = K_0 XOR (take B $ repeat 0x36) ++ text
H_K0_ipad_text = H(K0_ipad_text)
K0_opad = K_0 XOR (take B $ repeat 0x56)
K0_opad_H_K0_ipad_text = K0_opad ++ H_K0_ipad_text
H_K0_opad_H_K0_ipad_text = H(K0_opad_H_K0_ipad_text)
参考
P4に詳しくやり方が乗っている
関連