Online SHA hash

SHA-1 is a 160-bit digest, written as 40 hex characters. SHA-224, SHA-256, SHA-384, and SHA-512 are SHA-2, at 56, 64, 96, and 128 hex characters. The input is the UTF-8 bytes of the text. The same text with the same algorithm always produces the same digest. A trailing newline, or é as one code point versus a letter plus a combining mark, changes the bytes and the digest. SHA-224 uses a different initial value and then keeps 224 bits. It is not SHA-256 with the tail removed. None of these five has a salt or a key.

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

  1. SHA-1 is 160 bits. Two hex characters are one byte, so the text is 40 characters.
  2. 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.
  3. 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

  1. 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.
  2. abc under SHA-256 starts with ba7816bf. abc under SHA-224 starts with 23097d22. Those openings do not match.
  3. 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

  1. 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.
  2. One extra newline is the extra byte 0A. The SHA-256 digest of hello is not the digest of hello followed by a newline.
  3. é 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

  1. 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.
  2. These five are fast enough to try in bulk. A password store wants a slow hash and a new salt each time.
  3. 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.

Common questions

How many characters is a SHA-256 hash?
SHA-256 is 256 bits, which is 32 bytes, which is 64 hex characters. SHA-1 is 40, SHA-224 is 56, SHA-384 is 96, and SHA-512 is 128. Two hex characters stand for one byte, and this spelling uses lowercase digits with no spaces and no 0x prefix. An uppercase copy is the same bytes. A string of a different length is a different algorithm, or it is not the whole digest.
What is the SHA-256 hash of hello?
The five letters hello are the UTF-8 bytes 68 65 6c 6c 6f. SHA-256 of those bytes is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824. Hello, with a capital H, is a different byte and a different digest. hello followed by a newline is also different, because the byte 0A was added. Hashing the hex text 68656c6c6f hashes those ten characters, not the five bytes of hello.
What is the SHA-256 hash of an empty string?
An empty message is zero bytes, and it still has a digest. SHA-256 of that message is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. The result is 64 hex characters. It is not a blank field and not an error. A space is one byte, 20, and that is a different message from the empty one.
What is the difference between SHA-1 and SHA-256?
SHA-1 is 160 bits and 40 hex characters. SHA-256 is 256 bits and 64 hex characters, and it is a SHA-2 function with a different compression structure. A collision against SHA-1 has been published, so SHA-1 is a poor choice wherever two different messages must not share a digest. SHA-256 of abc starts with ba7816bf. SHA-1 of abc is a9993e364706816aba3e25717850c26c9cd0d89d. Length alone separates them.
Is SHA-512 just a longer SHA-256?
No. SHA-256 uses 32-bit words and a 512-bit block. SHA-512 uses 64-bit words and a 1024-bit block. The digest of abc under SHA-256 starts with ba7816bf and is 64 hex characters. Under SHA-512 it starts with ddaf35a1 and is 128 hex characters. The longer string is not the shorter string with more digits attached. On a 64-bit machine SHA-512 can also move more of a long message per round, which is a property of the word size, not of the digest being safer by itself.
Is SHA-224 the first 224 bits of SHA-256?
No. Both use the SHA-256 compression function, a 512-bit block, and 32-bit words, but SHA-224 starts from a different initial value and then keeps 224 bits. The SHA-224 digest of abc is 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7. The SHA-256 digest of abc starts with ba7816bf, so cutting that result would not produce the SHA-224 string. SHA-384 is the same kind of truncation of the SHA-512 compression function, again with its own initial value.
Can a SHA-256 digest be turned back into the text?
No. The digest is a fixed length, and many messages share the path into those bits. Nothing in the 64 hex characters records the length of the text or a way to walk backward. A match means the UTF-8 bytes were the same under that algorithm. It does not reveal the text, and it does not prove who produced the digest. A key is what turns the construction into HMAC. A salt and a cost are what a password store adds, and they are not part of SHA-256.
Why does the digest change when I only add a newline?
The newline is a byte. In UTF-8 that byte is 0A. hello is five bytes and its SHA-256 digest is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824. hello plus a newline is six bytes and the digest is 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03. A file checksum often includes that final newline. A text field does not, unless the newline was typed. The visible word can match while the byte strings do not.