Verifying Checksums and Encoding Bytes

A checksum, or digest, summarizes a file's bytes so that you can later check whether they changed. An encoding represents bytes as printable text so that they can pass through an interface that carries only text. Neither proves who produced the data: that needs a trusted source for the expected value, or a signature.

The examples use sample file names such as release.txt. Create those files first, or substitute your own.

Record and verify digests #

Record a SHA-256 digest of a file, then check the file against it:

sha256sum release.txt > checksums.txt
sha256sum -c checksums.txt

The check reports each file and whether its bytes still match, and fails if any doesn't. Anyone who can replace both the file and its checksum list can make the check pass, so get the expected digest through a channel you trust, or verify a signature with a trusted key.

cksum offers several algorithms through one command. Name the algorithm so that a saved result says what it means:

cksum -a sha256 release.txt

The default cksum algorithm and the older sum checksum detect accidental changes, such as transmission errors. They have too few possible values to identify a file securely, so use a cryptographic digest such as SHA-256 for new work.

Encode and decode bytes #

Encode a file as Base64 text, then decode it:

base64 original.bin > encoded.txt
base64 -d encoded.txt > decoded.bin

base32 works the same way with a different alphabet. basenc selects among several encodings, such as --base64url, the variant used in web addresses. Use the encoding the receiving program expects: Base64 and its URL-safe form look alike but aren't interchangeable.

Encoding isn't encryption: anyone with the text can decode it. A decoder can write part of its output before it finds invalid input, so check its exit status, and don't let another program use the output until the whole decode has succeeded.

Commands on this page #

This page covers these commands:

CommandUse it toManual
sha256sum and the other digest commandsRecord and check file digestssha256sum(1)
cksumCalculate a checksum with a chosen algorithmcksum(1)
sumCalculate an older checksum another system requiressum(1)
base64, base32Encode bytes as textbase64(1), base32(1)
basencEncode with a chosen alphabetbasenc(1)

Edit this page