What the five SHA lengths say about the UTF-8 bytes
SHA-1 produces 160 bits, written as 40 hex characters. SHA-224, SHA-256, SHA-384, and SHA-512 are SHA-2. Their hex forms are 56, 64, 96, and 128 characters. The input is the UTF-8 bytes of the text. hello is the five bytes 68 65 6c 6c 6f. Its SHA-256 digest is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824. The same text and the same algorithm always produce that digest, because none of these five mixes in a salt or a key.
The empty string is a real message. Its SHA-256 digest is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, still 64 characters, not a blank. Adding one newline after hello adds the byte 0A, and the SHA-256 digest becomes 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03. é as the single code point U+00E9 is the UTF-8 bytes C3 A9. The letter e plus a combining acute is 65 CC 81. The glyphs can look alike. The digests do not.
SHA-224 and SHA-256 share a 512-bit block and 32-bit words, but the initial value differs, and SHA-224 keeps only 224 bits. The SHA-256 digest of abc starts with ba7816bf. The SHA-224 digest of abc starts with 23097d22. The shorter one is not the first 56 characters of the longer one. SHA-384 and SHA-512 share a 1024-bit block and 64-bit words in the same way. The hex spelling of a digest is the subject of hex encode and decode. A keyed tag is HMAC. Storing a password wants a slow hash: bcrypt.
How bit length, the initial value, and the UTF-8 bytes decide the digest
Why the five algorithms write 40, 56, 64, 96, and 128 hex characters
- SHA-1 is 160 bits. Two hex characters are one byte, so the text is 40 characters.
- SHA-224, SHA-256, SHA-384, and SHA-512 are 224, 256, 384, and 512 bits, which is 56, 64, 96, and 128 hex characters.
- Changing the algorithm changes both the length and the value. The 64 characters of SHA-256 are not the first half of the 128 characters of SHA-512.
Why SHA-224 is not SHA-256 with the last 32 bits cut off
- SHA-224 and SHA-256 both use a 512-bit block and 32-bit words. The initial value differs, and SHA-224 keeps 224 bits.
- abc under SHA-256 starts with ba7816bf. abc under SHA-224 starts with 23097d22. Those openings do not match.
- SHA-384 stands to SHA-512 the same way: a 1024-bit block, 64-bit words, a different initial value, then 384 bits kept.
Why text that looks the same can still produce two digests
- The function reads UTF-8 bytes. It does not hash the shape on the screen, and it does not decode the text as hex before hashing.
- One extra newline is the extra byte 0A. The SHA-256 digest of hello is not the digest of hello followed by a newline.
- é as one code point is C3 A9. e plus a combining acute is 65 CC 81. Hello and hello are different bytes too.
Why a bare digest cannot store a password and is not HMAC
- The same message always yields the same digest. There is no salt, so two people who chose the same password land on the same value.
- These five are fast enough to try in bulk. A password store wants a slow hash and a new salt each time.
- HMAC mixes in a key first. A digest with no key can show that the bytes did not change. It cannot show who computed it.
Where the published digest and the typed bytes quietly disagree
The published test vector for the three letters abc
The SHA-256 digest of abc is ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. Dropping a letter, or switching to SHA-1, misses those 64 characters. SHA-1 of abc is a9993e364706816aba3e25717850c26c9cd0d89d, and that string is 40 characters. The letters did not gain a key or a salt between the two algorithms.
A checksum that included the newline at the end of the file
hello in a text field is five bytes, 68 65 6c 6c 6f. A file often stores hello and then a newline. The SHA-256 digest moves from 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 to 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03. One byte, 0A, replaces the whole digest. The hex form of those bytes is what hex encode and decode spells. The digest is a function of the bytes, not of the line as a person reads it.
A login table that stored one SHA-256 and called it enough
A single SHA-256 of a password is the same for every copy of that password, and it is fast to recompute. None of these five algorithms adds a salt or an iteration count. A keyed check is HMAC. A password store wants a slow hash, which is the subject of bcrypt. The digest here only names the bytes that were hashed.