hashing
Why Your SHA-256 Doesn't Match the Terminal: echo Adds a Newline
echo hello | sha256sum hashes six bytes, not five: the trailing newline. Real digests for echo, printf, CRLF, BOM, here-strings and UTF-16, plus the fix for each.
By Kyle Hurst ·
You hash hello in an online tool and get 2cf24dba…. You run echo "hello" | sha256sum and get 5891b5b5…. One of them must be broken. Neither is: echo printed hello plus a newline, so the terminal hashed six bytes and the tool hashed five. Use printf '%s' "hello" | sha256sum instead and the two will agree.
That is the most common cause. The rest of this post covers the other ways the same visible text turns into different bytes, with the digest each one produces, so you can match the hash you got against the table and know which mistake you made.
Everything below was run on macOS 27.0 with zsh 5.9, shasum 6.02, the system md5, and the sha256sum that macOS now ships (it reports itself as sha256sum (Darwin) 1.0). The digests don’t depend on the OS: the same bytes give the same hash everywhere.
The proof: look at the bytes
A hash function sees bytes, not text. xxd shows you exactly what went in:
$ echo "hello" | xxd
00000000: 6865 6c6c 6f0a hello.
$ echo "hello" | sha256sum
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 -
$ printf '%s' "hello" | xxd
00000000: 6865 6c6c 6f hello
$ printf '%s' "hello" | sha256sum
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 -
That trailing 0a is a line feed (LF, \n). echo ends its output with one unless told not to, because it was built to print lines to a screen. The hash function does not know the byte is “just formatting”, so it changes the whole digest.
Every common variant, with its digest
Same word, seven ways of feeding it to a hash:
| How the string was produced | Bytes hashed (hex) | SHA-256 | MD5 |
|---|---|---|---|
printf '%s' hello | 68 65 6c 6c 6f | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | 5d41402abc4b2a76b9719d911017c592 |
echo -n hello (bash, zsh) | 68 65 6c 6c 6f | 2cf24dba…938b9824 (same) | 5d41402a…1017c592 (same) |
echo hello | 68 65 6c 6c 6f 0a | 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 | b1946ac92492d2347c6235b4d2611184 |
printf '%s\n' hello | 68 65 6c 6c 6f 0a | 5891b5b5…46f6be03 (same as echo) | b1946ac9…d2611184 |
cat <<< hello (here-string) | 68 65 6c 6c 6f 0a | 5891b5b5…46f6be03 (same as echo) | b1946ac9…d2611184 |
CRLF line ending, hello\r\n | 68 65 6c 6c 6f 0d 0a | cd2eca3535741f27a8ae40c31b0c41d4057a7a7b912b33b9aed86485d1c84676 | af5597c29467a96523a70787c319f4db |
UTF-8 BOM, then hello | ef bb bf 68 65 6c 6c 6f | 7489ebbcc2a00056ddaaaac190bce473e5c03696ea1bd8ed83cf59a174283862 | 86c56c004b157e0e2b43d9561bada3b5 |
The here-string catches people who think they avoided echo. Bash and zsh both add a newline to the end of a <<< string, so sha256sum <<< "hello" gives the same answer as echo hello | sha256sum.
If your digest is in the 5891b5b5 row, you hashed a trailing LF. If it is cd2eca35, your text has a Windows line ending. If it is 7489ebbc, an invisible byte order mark rode along at the front, usually because the text came out of a file saved by a Windows editor.
Two digests worth memorising
These two turn up constantly in logs and bug reports. When you see one, you know the input was empty, or empty plus a newline.
| Input | SHA-256 | MD5 |
|---|---|---|
| Nothing at all (0 bytes) | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 | d41d8cd98f00b204e9800998ecf8427e |
A single \n (1 byte) | 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b | 68b329da9893e34099c7d8ad5cb9c940 |
$ printf '' | sha256sum
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -
$ printf '\n' | md5
68b329da9893e34099c7d8ad5cb9c940
e3b0c442… in particular is the classic sign of a bug upstream. A variable that was never set, a file that was read before it was written, or a request body that was already consumed all hash to it. 68b329da… usually means echo "$VAR" ran with $VAR empty.
echo -n is not portable, and macOS proves it
The obvious fix is echo -n, and in an interactive bash or zsh it works. But the POSIX standard for echo says that if the first operand is a - followed by any of e, E or n (so -n, -e and -ne all count), or if any operand contains a backslash, “the results are implementation-defined.” Its own advice is “New applications are encouraged to use printf instead of echo.”
Here is what that means in practice. We ran the same command through every shell on this Mac:
$ /bin/bash -c 'echo -n hello' | xxd
00000000: 6865 6c6c 6f hello
$ /bin/zsh -c 'echo -n hello' | xxd
00000000: 6865 6c6c 6f hello
$ /bin/dash -c 'echo -n hello' | xxd
00000000: 6865 6c6c 6f hello
$ /bin/sh -c 'echo -n hello' | xxd
00000000: 2d6e 2068 656c 6c6f 0a -n hello.
$ /bin/sh -c 'echo -n hello' | sha256sum
1428360b4af98bce66b16e40727110404148c47890a27e4a7e5972a2850686fa -
macOS’s /bin/sh is bash 3.2.57 running in POSIX mode, and its echo prints -n as literal text and still adds the newline. You get a third digest that matches neither. This is not just a curiosity for people who type sh by hand. Anything that runs commands through /bin/sh behaves the same way. On this machine, a Makefile recipe echo -n hello | xxd printed -n hello. under GNU Make 3.81. Python’s subprocess.run("echo -n hello", shell=True) returned b'-n hello\n'. So a script can work in your terminal and produce a different hash in CI or in make.
Backslashes vary too. echo 'a\tb' printed a literal backslash-t in /bin/bash but a real tab in both /bin/sh and zsh, which interprets escapes by default. If the string you hash contains a backslash, echo can change it before the hash ever sees it.
Use printf '%s' "$value" whenever the exact bytes matter. The %s format prints the argument as-is, with no escape processing and no newline, in every POSIX shell.
Files and command substitution
The newline also hides in files. echo hello > hello.txt writes six bytes, and every editor that “ends files with a newline” does the same. So hashing the file gives the 5891b5b5 digest, while hashing the string you see inside it gives 2cf24dba:
$ echo hello > hello.txt
$ sha256sum hello.txt
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 hello.txt
$ s=$(cat hello.txt); printf '%s' "$s" | sha256sum
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 -
The second command matches the online tool because $(...) strips trailing newlines from what it captures. That is the reverse surprise. A value read into a shell variable loses its newline, while the same value hashed straight from the file keeps it. In code the same split happens between reading a file as bytes and reading it as text and calling .strip(). In Python, hashlib.sha256(open("hello.txt","rb").read()) gave 5891b5b5…, and the .read().strip().encode() version gave 2cf24dba…. Decide which one you mean and write it down next to the hash.
For verifying downloaded files rather than strings, see how to verify a SHA-256 checksum on Windows, Mac and Linux. Files have their own pitfalls, and that post covers them.
Windows: UTF-16 and CRLF
We could not run Windows for this post. The behaviour below comes from Microsoft’s documentation, not from our own measurements. The digests are ones we computed from the bytes those docs describe.
PowerShell strings are UTF-16 inside. .NET, which PowerShell is built on, defines a String as a sequence of Char objects, and “a System.Char object corresponds to a UTF-16 code unit.” If you turn a string into bytes with [Text.Encoding]::Unicode.GetBytes("hello"), you get UTF-16LE, a different input altogether:
Encoding of hello | Bytes | SHA-256 |
|---|---|---|
| UTF-8 | 68 65 6c 6c 6f | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 |
| UTF-16LE | 68 00 65 00 6c 00 6c 00 6f 00 | 06e44dc1b95c469f43aaccb49e93c36827626266eed5575eced74af9a016c9cd |
| UTF-16LE with BOM and CRLF | ff fe 68 00 … 0d 00 0a 00 | 99f86e1be6de5ae82b3f23977b65ebe43cb8359b37b6cf0ab8e6c2eaa0c99193 |
That last row matters because, per Microsoft’s about_Character_Encoding, in Windows PowerShell 5.1 “Out-File and the redirection operators > and >> create UTF-16LE,” and “any Unicode encoding, except UTF7, always creates a BOM.” PowerShell 6 and later default to utf8NoBOM. So "hello" > hello.txt writes different bytes depending on which PowerShell you opened.
Get-FileHash hashes files or streams. The Get-FileHash documentation says plainly that “PowerShell does not provide a cmdlet to compute the hash of a string” and gives this workaround:
$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$writer.Write("Hello world")
$writer.Flush()
$stringAsStream.Position = 0
Get-FileHash -InputStream $stringAsStream | Select-Object Hash
The docs show 64EC88CA00B268E5BA1A35678A1B5316D212F4F366B2477232534A8AECA37F3C as the result. We got the same digest from printf 'Hello world' | shasum -a 256 on the Mac, which fits the StreamWriter docs: that constructor uses “UTF-8 encoding without a Byte-Order Mark.” Use Write, not WriteLine, or you are back to hashing a line ending.
certutil only hashes files. Its documented syntax is certutil [options] -hashfile InFile [HashAlgorithm] (certutil reference). To hash a string with it you first write the string to a file, and the line ending and encoding of that file then decide the result.
CMD’s echo. Microsoft’s echo reference does not say what line ending echo writes, or what happens to the space in echo hello > file.txt. It is commonly reported that CMD writes CRLF and keeps the space before >. We could not confirm either from Microsoft’s docs or test it. If it happens, the file holds hello \r\n (68 65 6c 6c 6f 20 0d 0a), whose SHA-256 is f873eef4f852e335da367d76ce7f1973c15b8ffebf532b064df4bc691cd51a87. If you see that digest, now you know why.
Same letters, different bytes: encoding and normalisation
Accented characters add another way to get different bytes. Unicode allows é to be stored as one code point (U+00E9, the NFC form) or as e followed by a combining acute accent (U+0065 U+0301, the NFD form). They render identically. They hash differently:
café as | UTF-8 bytes | SHA-256 |
|---|---|---|
| NFC (precomposed) | 63 61 66 c3 a9 | 850f7dc43910ff890f8879c0ed26fe697c93a067ad93a7d50f466a7028a9bf4e |
| NFD (decomposed) | 63 61 66 65 cc 81 | 81ef060bcd98adc7824eb5c1ada83c32491b16018e11e79f00ab9d09e04b015a |
| Latin-1, not UTF-8 | 63 61 66 e9 | dafd66c0b98965e688be1fc12942c09f0350e6be0685017c3f234e97d0adc92e |
(Computed with Python 3.15’s unicodedata.normalize and checked against Node 22’s crypto.) If two systems must agree on a hash of human-typed text, normalise it first, for example with s.normalize('NFC') in JavaScript, and say so in the spec. Zero-width spaces and other invisible characters do the same damage. They are covered in invisible Unicode characters, and the Invisible Character Detector will find them.
Uppercase hex is the same digest
One thing that is not a mismatch: 2CF24DBA… and 2cf24dba… are the same hash. Hex is a way of writing bytes, and C and c both mean twelve. Get-FileHash prints uppercase. sha256sum, shasum and most libraries print lowercase. Compare case-insensitively, or lowercase both sides first. If one side is base64 (LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= is the same hello digest), decode it before you compare.
What our tools hash
Our Hash Generator runs TextEncoder().encode() on the textarea’s contents. That gives the raw UTF-8 bytes of exactly what is in the box. It adds no newline, does not trim spaces, and does not apply Unicode normalisation. SHA-1, SHA-256 and SHA-512 come from the browser’s Web Crypto API. MD5 uses our own implementation, which we checked against Node’s crypto for every input length from 0 to 1,199 bytes, ASCII and multi-byte, with no mismatches. Typing hello gives 2cf24dba…, the printf '%s' row.
Two consequences follow from that design:
- Pressing Enter adds a byte. Type
hello, press Enter, and the tool shows5891b5b5…, theechodigest, because the box now really containshello\n. The same applies if a paste ends with a line break. That makes it a quick way to check which form your terminal hashed. - Line endings in the box are always LF. The HTML standard says a textarea’s value is its raw value with newlines normalized, and the Infra standard defines that as turning every CR LF pair, and every lone CR, into LF. So you cannot reproduce a CRLF digest by pasting Windows text into any browser textarea, ours included. You get the LF version.
The Hash Generator also shows nothing for an empty box, so use the table above for the empty-string digests. To see the exact bytes a string will produce before you hash it, paste it into Text to Hex, which prints the same UTF-8 bytes in the same order.
When the input is a file (a CRLF text file, a BOM-prefixed export, a UTF-16 PowerShell redirect), use the File Checksum Verifier instead. It reads the file’s bytes in chunks and hashes them unchanged. It never decodes them as text, so nothing gets normalised. It supports MD5, SHA-1, SHA-256, SHA-512, SHA3-256, BLAKE3 and CRC32. When you paste an expected digest, it ignores case, spaces and colons. Both tools run entirely in your browser.
If you are hashing a string with a secret key to sign a request, that is an HMAC, not a plain hash. The same newline and encoding rules apply, and how HMAC works covers the rest.
The short checklist
When two SHA-256 or MD5 digests of “the same” string disagree:
- Is one of them
5891b5b5…-style, the hash of your text plus\n? Switchechotoprintf '%s'. - Pipe both inputs through
xxd(orod -c) and compare the bytes, not the text. - Look for
0d 0a(CRLF),ef bb bf(UTF-8 BOM),ff fe(UTF-16LE BOM) or00after every letter (UTF-16). - For accented text, check NFC vs NFD.
- Lowercase both digests before comparing.