Cryptographic Hashing Explained: Linux Commands and Examples.

Learn how cryptographic hashing works, explore SHA-256 and SHA-512 on Linux, compare hashing with encryption, and verify file integrity using practical command-line examples.

Cryptographic Hashing Explained: Linux Commands and Examples.
Illustration by SNUBmonkeyteam


Cryptographic hashing is a fundamental concept in Linux administration, cybersecurity, and data integrity. It allows you to generate a fixed-length value from data of almost any size, making it possible to verify whether a file has changed, detect accidental corruption, and support various security mechanisms.

Linux provides several command-line utilities for calculating and verifying cryptographic hashes. These tools are useful when downloading software, transferring files, troubleshooting data corruption, or validating backups.

In this tutorial, you'll learn what cryptographic hashing is, how it works, how to calculate hashes on Linux, and how to use them to verify file integrity.

What Is Cryptographic Hashing?

A cryptographic hash function takes an input—such as a text string, document, executable, or disk image—and processes it to produce a fixed-length output called a hash, hash value, or message digest.

For example, a SHA-256 hash always produces a 256-bit result, regardless of the size of the original input.

Consider the following text:

Hello World

Calculating its SHA-256 hash produces:

$ echo -n "Hello World" | sha256sum
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e  -

The output is a hexadecimal representation of the 256-bit hash.

The echo -n command prevents the shell from appending a newline to the input. Without -n, the newline becomes part of the data being hashed, producing a different result.

A hash is not an encrypted version of the original text. It is a one-way transformation designed to make recovering the original input computationally impractical for a secure cryptographic hash function.

How Does a Cryptographic Hash Function Work?

A cryptographic hash function processes input data according to a defined algorithm and produces a digest of a fixed size.

A secure cryptographic hash function has several important properties.

Deterministic output

The same input always produces the same hash when processed using the same algorithm.

Fixed-length output

The output length remains constant regardless of the input size. SHA-256 always produces a 256-bit digest, while SHA-512 produces a 512-bit digest.

Preimage resistance

Given a hash value, it should be computationally impractical to determine an input that produces that hash.

Second-preimage resistance

Given a particular input, it should be computationally impractical to find a different input with the same hash.

Collision resistance

It should be computationally impractical to find any two distinct inputs that produce the same hash.

Avalanche effect

A small change in the input should produce a substantially different-looking hash.

For example, these two strings differ by only one character:

$ echo -n "Hello World" | sha256sum
$ echo -n "hello World" | sha256sum

The resulting hashes are dramatically different.

This behavior is useful for detecting changes to files and other data. However, a matching hash alone does not prove that a file is trustworthy; that depends on whether the reference hash itself comes from a trusted source.

Hashing vs. Encryption

Hashing and encryption are both used in information security, but they serve different purposes.

Hashing is designed to produce a fixed-length digest. It is generally a one-way operation: there is no decryption key that reverses a secure cryptographic hash.

Encryption transforms readable data into ciphertext using a cryptographic algorithm and key. With the appropriate key and algorithm, the original data can be recovered.

For example, hashing is useful for checking whether a downloaded file matches an expected digest. Encryption is useful for protecting the contents of a file so that only authorized parties can read it.

These operations should not be confused. If you need to recover the original information, encryption—not hashing—is the appropriate mechanism.

Common Cryptographic Hash Algorithms

Several hashing algorithms have been used across operating systems, applications, and security tools.

MD5

MD5 produces a 128-bit hash, commonly represented as 32 hexadecimal characters.

Although it remains available on many Linux systems, MD5 is no longer considered secure against deliberate collision attacks. It should not be used where collision resistance is a security requirement.

It may still appear in legacy systems and older documentation, but stronger alternatives should be preferred.

SHA-1

SHA-1 produces a 160-bit hash, represented as 40 hexadecimal characters.

SHA-1 has known practical collision weaknesses and should not be used for new security-sensitive applications that depend on collision resistance.

SHA-256

SHA-256 belongs to the SHA-2 family and produces a 256-bit hash, represented as 64 hexadecimal characters.

It is widely used for file verification, software distribution, and other integrity-related operations.

SHA-512

SHA-512 also belongs to the SHA-2 family. It produces a 512-bit hash, represented as 128 hexadecimal characters.

It is available through standard Linux utilities and is useful when a SHA-512 digest is required.

For general-purpose file integrity verification, SHA-256 and SHA-512 are sensible choices. The best algorithm depends on the security requirements and the compatibility of the systems involved.

Calculating Hashes on Linux

Linux includes command-line utilities for calculating cryptographic hashes without requiring additional software on most distributions.

Calculate a SHA-256 Hash

Use sha256sum to calculate the SHA-256 hash of a file.

For example:

$ sha256sum ubuntu.iso

Example output:

d41d8cd98f00b204e9800998ecf8427e  ubuntu.iso

The displayed hash is illustrative, not the actual SHA-256 digest of an Ubuntu ISO. The value above resembles a well-known MD5 digest, so do not use it as a verification reference.

To calculate the real hash, run the command on your own file.

Calculate a SHA-512 Hash

To calculate a SHA-512 hash, use:

$ sha512sum ubuntu.iso

The command prints the 512-bit digest followed by the filename.

Both utilities read the file's contents and calculate the hash. They do not modify the original file.

Calculate a Hash from Text

You can also hash a text string without creating a file:

$ printf '%s' 'SNUBmonkey' | sha256sum

Using printf avoids adding a trailing newline, making the input explicit.

For comparison, try:

$ printf '%s\n' 'SNUBmonkey' | sha256sum

These commands produce different hashes because the second command includes a newline character.

When reproducing a hash, ensure that the input is identical—including spaces, line endings, and other characters.

Verifying File Integrity with SHA-256

One of the most practical uses of cryptographic hashing is verifying that a file has not changed.

Suppose you download an ISO image or an application package. The software publisher provides an official SHA-256 checksum through a trusted distribution channel.

First, calculate the hash of the downloaded file:

$ sha256sum ubuntu.iso

Compare the resulting digest with the official checksum.

If they match, the file's contents match the data represented by that reference digest, assuming the comparison is performed correctly.

If they differ, the file may have been corrupted, modified, downloaded incorrectly, or compared against the wrong reference value.

You can also automate the comparison using a checksum file.

Create a file named SHA256SUMS containing the expected digest and filename:

<expected-sha256-digest>  ubuntu.iso

Replace <expected-sha256-digest> with the actual digest supplied by the trusted source.

Then run:

$ sha256sum -c SHA256SUMS

A successful verification reports:

ubuntu.iso: OK

If verification fails, investigate before using the file.

Important: A checksum is only as trustworthy as its source. If an attacker can replace both the downloaded file and the checksum, a simple comparison will not detect the substitution. For stronger assurance, obtain checksums from a trusted source and verify signed checksums or digital signatures when available.

Hashing and Digital Signatures

Cryptographic hashes are also important in digital-signature systems.

Rather than directly signing an entire large document, many signature schemes operate on a cryptographic digest of the data. The signature process then uses a private key, while verification uses the corresponding public key and the relevant signature algorithm.

This helps establish whether signed data has been altered and whether the signature validates under the expected public key.

However, a hash by itself is not a digital signature. Anyone can calculate a hash for a file. A digital signature provides additional authentication properties when the signing key and verification process are trustworthy.

This distinction matters when verifying software packages, signed releases, and other security-sensitive files.


Best Practices for Using Cryptographic Hashes

When working with cryptographic hashes on Linux, follow these practices:

  • Prefer SHA-256 or SHA-512 for general-purpose integrity checks instead of MD5 or SHA-1 where collision resistance matters.
  • Verify against a trusted reference. A checksum downloaded from an untrusted location may not provide meaningful assurance.
  • Use digital signatures when authenticity matters. A matching checksum does not independently establish who produced a file.
  • Be consistent about the input. Extra spaces, newline characters, and different file contents produce different hashes.
  • Do not treat ordinary hashing as password protection. Password storage requires specialized password-hashing methods designed to resist guessing attacks.

Cryptographic hashing is essential for Linux administration and cybersecurity. Tools like sha256sum make it easy to calculate digests, compare files, and detect changes.
Understanding how hashing differs from encryption and digital signatures ensures you choose the right tool for the job. While hashing is also key to password security, standard hashes aren't enough on their own—they require extra protections against guessing attacks.
In the next tutorial, we'll explore Linux password hashing, including salting, /etc/shadow, algorithms, and credential protection.

Thanks for reading, and stay tuned for more Linux, infrastructure, and cybersecurity tutorials from SNUBmonkey™.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers