filesdataFormats and data

02 / Hashes

Hashes check bytes, not promises

A checksum helps compare copies. It neither reveals the contents nor proves who created a file on its own.

What a hash computes

A hash function takes a sequence of bytes and produces a short result. SHA-256 has a 256-bit output, usually written as 64 hexadecimal characters. Changing even one byte will generally produce a completely different result. Renaming a file without changing its contents does not change its hash.

This is useful after downloading, transferring to another drive or extracting an archive. Matching SHA-256 hashes give very high practical confidence that two files contain the same bytes, but a hash is not a mathematically unique identifier for every possible file. Different inputs can theoretically share an output.

Choose an algorithm

AlgorithmUseful forLimitation
CRC32Detecting accidental transmission errorsNot protection from deliberate tampering
MD5, SHA-1Comparison with older inventoriesNot a choice for new tamper-resistant checks
SHA-256New inventories and integrity checksRequires a trusted reference

SHA-256 belongs to the SHA-2 family. NIST also specifies a separate SHA-3 family; it is not simply a longer spelling of SHA-2.[4] SHA-256 is a straightforward, interoperable choice for an ordinary file inventory.

Compute locally

These commands only read the specified file. Replace the name with your own; quote paths containing spaces. Use an existing file that is not being edited at the same time.

Linux

sha256sum "example.txt"

macOS

shasum -a 256 "example.txt"

Windows PowerShell

Get-FileHash -Algorithm SHA256 -LiteralPath "example.txt"

Compare the entire output, not only the first and last characters. Uppercase and lowercase A–F mean the same thing in hexadecimal notation. An extra line ending inside the file, however, changes its bytes.

One line, an exact result

Take six bytes: the ASCII word hello followed by one LF line ending. Without the LF the hash is different. There is no BOM, space or CR character.

Bytes: 68 65 6c 6c 6f 0a
SHA-256:
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03

The example is computed from b'hello\n' when this page is built. Do not rely on a visually similar line copied from an editor for an exact comparison: the editor may use a different line ending.

Find a trusted reference

If a file and its checksum are changed together, a comparison will not detect that. To resist tampering, the reference must come from a trusted source or be confirmed by a verifiable digital signature. A plain hash does not replace a signature, certificate or provenance check.

Hashing does not encrypt a file. A fast general-purpose SHA-256 hash is also not a complete password-storage scheme: passwords need purpose-built algorithms with salts and a work factor. Treat a file hash as a tool for comparing contents, not as a universal mark of safety.

Sources