What a key changes in a hash of the same UTF-8 bytes
HMAC mixes a key into a hash. The algorithms here are MD5, SHA-1, SHA-256, SHA-384, and SHA-512. The key and the message are both the UTF-8 bytes of the text that was typed. The key is not decoded as hex first. The tag has the length of that hash: 32, 40, 64, 96, or 128 hex characters, which is 24, 28, 44, 64, or 88 Base64 characters. SHA-384 is 48 bytes, so its Base64 form has no padding character.
The key key and the message abc produce the HMAC-SHA-256 tag 9c196e32dc0175f86f4b1cb89289d6619de6bee699e4c378e68309ed97a1a6ab. In Base64 that same tag is nBluMtwBdfhvSxy4konWYZ3mvuaZ5MN45oMJ7Zehpqs=. SHA-256 of abc alone starts with ba7816bf. SHA-256 of the concatenation keyabc is 1141e2212b08bb07b3c9558bb7a90f4a99b1c6f0c97e970c279df64e7f0875ed. Neither string is the HMAC tag. The same key, the same message, and the same algorithm always repeat the tag, because there is no salt.
A key shorter than the block is padded with zero bytes. MD5, SHA-1, and SHA-256 use a 64-byte block. SHA-384 and SHA-512 use a 128-byte block. A key longer than the block is hashed once, and that digest is then padded. The inner pad byte is 36 hex and the outer pad byte is 5c hex. The message stays readable. HMAC does not encrypt it. A password store wants a slow hash, which is the subject of bcrypt. A bare digest is hash digest. The hex spelling of a tag is hex encode and decode.
How the key, the block, and the UTF-8 bytes decide the tag
Why HMAC-MD5 through HMAC-SHA-512 write 32, 40, 64, 96, and 128 hex characters
- MD5 is 128 bits, SHA-1 is 160, SHA-256 is 256, SHA-384 is 384, and SHA-512 is 512. Two hex characters are one byte.
- Those lengths are 32, 40, 64, 96, and 128 hex characters. Base64 of the same tags is 24, 28, 44, 64, and 88 characters.
- HMAC-SHA-512 of key and abc starts with 3926a207. It is not HMAC-SHA-256 with extra characters stuck on the end.
Why hashing the key and the message concatenated is not HMAC
- HMAC hashes the inner pad mixed with the key, then hashes the outer pad mixed with that inner digest and the message.
- SHA-256 of the six characters keyabc is 1141e2212b08bb07b3c9558bb7a90f4a99b1c6f0c97e970c279df64e7f0875ed. HMAC-SHA-256 of key and abc starts with 9c196e32.
- Putting the message first, or inserting a colon between the key and the message, is a third string. The tag moves either way.
Why the key is the UTF-8 bytes of the text, not a hex decoding
- The three letters key are the bytes 6b 65 79. The six characters 6b6579 are the bytes of those digits and letters.
- HMAC-SHA-256 of the key 6b6579 and the message abc is cdd1e9711b2f531b9459a6a04b988d405f138bff447236e33d556b56d9e5106f. That is not the tag of the key key.
- A newline is the byte 0A. The key key plus a newline, with the message abc, starts the HMAC-SHA-256 tag at e5166670.
Why HMAC neither hides the message nor stores a password
- The message is an input to the tag. The tag does not replace the message, and anyone who can read the message can still read it.
- The same key and the same message always yield the same tag. There is no salt, and these hashes are fast enough to try in bulk.
- A password store wants a slow hash and a new salt each time. A tag with no key is only a digest, and it cannot show who produced it.
Where the published tag and the typed key quietly disagree
The three letters abc under the three-letter key key
HMAC-SHA-256 of the key key and the message abc is 9c196e32dc0175f86f4b1cb89289d6619de6bee699e4c378e68309ed97a1a6ab. HMAC-SHA-1 of that same pair is 4fd0b215276ef12f2b3e4c8ecac2811498b656fc, 40 hex characters. HMAC-MD5 is d2fe98063f876b03193afb49b4979591, 32 hex characters. The letters did not change between the three algorithms. Only the hash inside the construction changed. The Base64 form of the SHA-256 tag is nBluMtwBdfhvSxy4konWYZ3mvuaZ5MN45oMJ7Zehpqs=.
A key that was pasted as the hex spelling of itself
The word key is three bytes, 6b 65 79. The text 6b6579 is six bytes, the characters 6, b, 6, 5, 7, and 9. HMAC-SHA-256 of that six-character key and the message abc is cdd1e9711b2f531b9459a6a04b988d405f138bff447236e33d556b56d9e5106f. A check that expected the three-byte key will not match. The hex spelling of a byte string is the subject of hex encode and decode. The tag here is a function of the bytes that were typed.
A login table that stored one HMAC-SHA-256 and called it enough
A shared key plus HMAC-SHA-256 of a password is the same tag for every copy of that password, and it is fast to recompute. There is no salt and no iteration count. A bare SHA-256 of the password is a different mistake, and it is the subject of hash digest. A password store wants a slow hash: bcrypt. HMAC can show that a message still matches a key. It does not hide the message, and it does not replace the password.