← Lessons/Encoding, Hashing, HMAC & Encryption
← Encoding, Hashing, HMAC & Encryption

Hash and check integrity

Learn

Hashing: a fingerprint for bytes

A cryptographic hash such as SHA-256 maps any input to a fixed 256-bit digest (64 hex characters):

  • Deterministic: the same bytes always give the same digest.
  • One-way: you can't recover the input from the digest.
  • Avalanche: change one bit of input and roughly half the output bits flip.
  • Collision-resistant: nobody can find two inputs with the same digest.
sha256sum config.txt          # <64 hex chars>  config.txt
sha256sum -c config.txt.sha256  # config.txt: OK

Release pages publish a SHA256SUMS file for exactly this reason: sha256sum -c tells you the download wasn't corrupted. Notice what it doesn't prove. If an attacker can replace the download, they can replace the checksum file too, because anyone can compute a hash. A hash gives you integrity but not authenticity.

MD5 and SHA-1 are broken for collision resistance. Don't use them for anything security-related. SHA-256 (or SHA-512, SHA-3, BLAKE2) is the modern default.

Print the SHA-256 digest of config.txt, then check it against the published checksum file config.txt.sha256 so the output says config.txt: OK.

$ bash solution.sh
Loading...

Output

Run your solution to see its output here.