You hash a string, compare it with the value someone sent you, and they don't match. The hash function is almost never the problem. A hash is calculated over bytes, not over the text you see, and the bytes are usually different.
1. The trailing newline
echo adds a newline. This is the most common one:
printf 'hello' | sha256sum # 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 echo 'hello' | sha256sum # 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 Same visible word, completely different hash. Use printf or echo -n when you mean just the text.
2. Windows line endings
A file saved on Windows usually ends lines with \r\n, not \n. printf 'hello\r\n' | sha256sum gives cd2eca35..., a third different value. If you hash files, check whether git or your editor converted line endings.
3. Text encoding
é is two bytes in UTF-8 and one byte in Latin-1, so héllo has two different hashes depending on the encoding. In code, be explicit:
const bytes = new TextEncoder().encode("héllo"); // always UTF-8 const digest = await crypto.subtle.digest("SHA-256", bytes); const hex = [...new Uint8Array(digest)] .map(b => b.toString(16).padStart(2, "0")).join(""); 4. Hex vs Base64 output
The same digest can be written as 64 hex characters or 44 Base64 characters. Compare like with like.
5. Uppercase hex
2CF24D... and 2cf24d... are the same hash. Lowercase both before comparing.
6. Hashing the wrong thing
Trimmed vs untrimmed input, a BOM at the start of a file, JSON with different key order or spacing: all change the bytes. If two systems must agree, agree on the exact bytes first.
Quick checklist
- Newline at the end?
-
\r\nvs\n? - UTF-8 everywhere?
- Hex vs Base64, upper vs lower case?
And a reminder: MD5 and plain SHA-256 are fine for checksums, but not for storing passwords. Use bcrypt, scrypt or Argon2 for that.
Disclosure: I run GoToolsOnline. If you want to check a value quickly, its free hash generator shows MD5, SHA-256 and SHA-512 of text.