Verifying If Two Files Are Truly Identical — Using SHA-256 in PowerShell

PowerShellカテゴリを表すパンダのイラスト PowerShell

About This Article
This article is organized based on verified public documentation from Microsoft Learn and NIST.

Verification Status: 📘 Official Specifications Confirmed / Windows Real Device Unverified
The provided code is for PowerShell. Because it has not been verified on a physical device yet, it is not labeled as “copy-paste execution verified.”

If you want to check whether the contents of two files are identical, a straightforward approach is to calculate their SHA-256 hashes using PowerShell’s Get-FileHash and compare the hash values. This allows you to compare values calculated from the actual content rather than relying on file names or modification timestamps.

Minimum Comparison

$hashA = (Get-FileHash -LiteralPath '.fileA.bin' -Algorithm SHA256).Hash
$hashB = (Get-FileHash -LiteralPath '.fileB.bin' -Algorithm SHA256).Hash

$hashA -eq $hashB

If True, the SHA-256 hashes match; if False, they do not match.

flowchart LR
    A[File A] --> C[SHA-256]
    B[File B] --> D[SHA-256]
    C --> E{Digestを比較}
    D --> E
    E -->|一致| F[同一内容の確認に使う]
    E -->|不一致| G[内容は異なる]

According to Microsoft Learn, the default algorithm for Get-FileHash is SHA256.

Verifying by Creating Dummy Files

Set-Content -LiteralPath '.fileA.txt' -Value 'papanda925 sample' -NoNewline
Copy-Item -LiteralPath '.fileA.txt' -Destination '.fileB.txt'

Get-FileHash -LiteralPath '.fileA.txt' -Algorithm SHA256
Get-FileHash -LiteralPath '.fileB.txt' -Algorithm SHA256

Since fileB is a copy of fileA, their hash values will match as long as the contents remain unchanged.

Next, let’s change just a single character.

Set-Content -LiteralPath '.fileB.txt' -Value 'papanda925 Sample' -NoNewline

(Get-FileHash '.fileA.txt').Hash
(Get-FileHash '.fileB.txt').Hash

Even a slight change in the input results in a significant difference in the hash value.

flowchart TB
    A["papanda925 sample"] --> B[SHA-256]
    C["papanda925 Sample"] --> D[SHA-256]
    B --> E[Digest A]
    D --> F[Digest B]
    E -. 入力が1文字違う .-> F

Same Size Does Not Mean Same Content

The following two items have the same length, but different contents.

ABCDEF
123456

While file size is useful for “quickly spotting obviously different files,” it cannot serve as a replacement for content comparison.

Including Existence Checks in Practical Code

$pathA = '.fileA.txt'
$pathB = '.fileB.txt'

foreach ($path in @($pathA, $pathB)) {
    if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
        throw "File not found: $path"
    }
}

$a = Get-FileHash -LiteralPath $pathA -Algorithm SHA256
$b = Get-FileHash -LiteralPath $pathB -Algorithm SHA256

[pscustomobject]@{
    FileA = $a.Path
    FileB = $b.Path
    HashA = $a.Hash
    HashB = $b.Hash
    Same  = ($a.Hash -eq $b.Hash)
}

To avoid situations where “comparing non-existent files makes subsequent processing appear to succeed,” we check the inputs beforehand.

Also Useful for Verifying Downloaded Files

If the distributor publishes a SHA-256 value, you can compare it against the value of the file you downloaded.

Get-FileHash -LiteralPath '.download.iso' -Algorithm SHA256

The key is not just comparing “two values you calculated yourself,” but rather cross-referencing them with the expected value published by a trusted distributor through a separate channel.

SHA-256 and Collisions

Cryptographic hash functions have a concept called a collision, where different inputs produce the same output value. Therefore, strictly speaking, it is not mathematically precise to state that “same hashes always mean identical files.”

On the other hand, SHA-256 is widely used for routine file integrity verification. Microsoft Learn also designates SHA256 as the default value for Get-FileHash, explaining that MD5 and SHA1 are no longer considered secure against attacks.

Hashing Is Not Encryption

SHA-256 is not a process for keeping file contents secret.

flowchart LR
    A[ファイル内容] --> B[SHA-256]
    B --> C[固定長のdigest]
    C -. 元ファイルを暗号化しているわけではない .-> D[秘密保持とは別]

Integrity verification and encryption serve different purposes.

Summary

  • You can calculate SHA-256 from file contents using Get-FileHash

  • Better suited for content comparison than relying solely on file names, dates, or sizes

  • Hash values change even with a single character modification

  • Can also be used to verify against SHA-256 values published by distributors

  • SHA-256 is an integrity check, not encryption for confidentiality

Official Information and Primary Sources

Microsoft Learn — Get-FileHash
https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-filehash

NIST — Secure Hash Standard (SHS)
https://csrc.nist.gov/pubs/fips/180-4/upd1/final

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

Copied title and URL