code q&a
Resources About
Question:

What is SHA-256?

SHA-256 turns any file or piece of text into a string of 64 letters and numbers that changes completely when you edit even one letter. It’s the most trusted hash function in everyday use, so it sits behind software downloads, secure websites and Bitcoin. I run into SHAs everywhere, mostly as Git commit IDs but also in crypto and JWTs. Git has used the older SHA-1 for most of its history, so those commit IDs are technically SHA-1 hashes.

SHA stands for Secure Hash Algorithm and the 256 is the size of the output in bits, the 1s and 0s of binary that computers store everything in. Those bits get written as 64 hexadecimal characters, which only use the digits 0-9 and the letters a-f, so every SHA-256 hash has the same length whether you feed it one word or a 4 GB video. The US National Security Agency designed it as part of a family called SHA-2 and the US standards agency NIST published it in 2001. Two different inputs with the same hash are called a collision. Nobody has ever found one for SHA-256, which is why it became the default choice for security work.

SHA-256 took over from MD5 and SHA-1, two older hash functions that attackers eventually broke. Researchers found MD5 collisions back in 2004. SHA-1 lasted until 2017, when Google and CWI Amsterdam used the SHAttered attack to produce two different PDF files with the same SHA-1 hash. Browsers stopped trusting SHA-1 certificates that same year, though most websites had already moved to SHA-256 by then. Git added an optional SHA-256 mode for the same reason.

Checking a download is the easiest way to see it in action. Lots of projects publish a SHA-256 checksum next to their files so you can compute your own in the terminal and compare the two:

shasum -a 256 ubuntu.iso   # macOS
sha256sum ubuntu.iso       # Linux

It also shows up in places you might not expect. A JWT signed with HS256 mixes a secret key into a SHA-256 hash to prove nobody tampered with the token. Bitcoin uses SHA-256 for mining and for the IDs of every block and transaction.

SHA-256 is fast, which is great for checking a big file and bad for storing passwords. An attacker who steals a database of SHA-256 password hashes can try billions of guesses per second on a single graphics card. Slow algorithms like bcrypt are built for that job instead.

You might also like