r/PowerShell • • Dec 07 '25

Question sha256 with Powershell - comparing all files

Hello, if I use

Get-ChildItem "." -File -Recurse -Name | Foreach-Object { Get-FileHash -Path $($_) -Algorithm SHA256 } | Format-Table -AutoSize | Out-File -FilePath sha256.txt -Width 300

I can get the checksums of all files in a folder and have them saved to a text file. I've been playing around with it, but I can't seem to find a way where I could automate the process of then verifying the checksums of all of those files again, against the checksums saved in the text file. Wondering if anyone can give me some pointers, thanks.

13 Upvotes

52 comments sorted by

View all comments

Show parent comment

0

u/Sea_Ice6776 Dec 08 '25 edited Dec 11 '25
  1. This is factually and demonstrably false, even on old-ish hardware. SHA256 is significantly faster than MD5 and even SHA1, and the gains are linearly proportional to input size. Depending on the CPU and the .net/PS version in use, SHA512 can be even faster, too. The only cost is the size of the hashes, which is pretty insignificant anyway.

  2. Get-FileHash, if given a wildcard path, already gives you an object. Just select out the Path and Hash properties into your JSON/CSV/whatever, and use that for future comparisons.

Here's proof of #1 for you:

# This will create 1000 1MiB files filled with random bytes, benchmark hash algorithms over them, and delete those files.
# Thus, you need slightly under 1GiB of free space to run this.
# This took 

using namespace System.IO
using namespace System.Buffers

# make a temp dir to operate in and set aside a 1MiB buffer for random data
$proofDir = [System.IO.Directory]::CreateTempSubdirectory('redditsha_')
pushd $proofDir
[byte[]]$buffer = [System.Buffers.ArrayPool[byte]]::Shared.Rent(1048576)

# Create 1000 files in the temp dir, each with 1MiB of random data
Range -Start 0 -Stop 1000 -Step 1 |% {
 [Random]::Shared.NextBytes($buffer)
 [System.IO.File]::WriteAllBytes("$PWD\File$_.tmp", $buffer)
}

# run the benchmarks, with each one getting 2 warmup runs before measuring
Range 0 2 1 |% { Get-FileHash -Algorithm SHA1 .\*.tmp }

$sha1results = Measure-Command -Expression {
  Range 0 20 1 |% { Get-FileHash -Algorithm SHA1 .\*.tmp }
}

Range 0 2 1 |% { Get-FileHash -Algorithm MD5 .\*.tmp }

$md5results = Measure-Command -Expression {
  Range 0 20 1 |% { Get-FileHash -Algorithm MD5 .\*.tmp }
}

Range 0 2 1 |% { Get-FileHash -Algorithm SHA256 .\*.tmp }

$sha256results = Measure-Command -Expression {
  Range 0 20 1 |% { Get-FileHash -Algorithm SHA256 .\*.tmp }
}

Range 0 2 1 |% { Get-FileHash -Algorithm SHA512 .\*.tmp }

$sha512results = Measure-Command -Expression {
  Range 0 20 1 |% { Get-FileHash -Algorithm SHA512 .\*.tmp }
}

$md5results,$sha1results,$sha256results,$sha512results | ft -AutoSize

Remove-Variable md5results
Remove-Variable sha1results
Remove-Variable sha256results
Remove-Variable sha512results

# Clean up the directory and its contents
popd
Remove-Item $proofDir -Recurse -Force
Remove-Variable proofDir

# Return the buffer to the pool
[System.Buffers.ArrayPool[byte]]::Shared.Return($buffer)
Remove-Variable buffer

On my machine, which has an older AMD Ryzen 7 5800X CPU (4 generations old at this point) SHA256 is a little more than twice as fast as SHA1, and SHA512 had the same runtime within less than 1%.

And this is single-threaded performance with the only difference being the hash algorithm.

SHA256 is not overkill.

Whoops. I didn't include MD5 in the above originally. Added now.
It consistently takes about 20% longer than SHA1 on my system, making it the worst of the bunch.

A non-trivial part of that is because the wider algorithms are processing larger chunks of data at once.

2

u/Sea_Ice6776 Dec 09 '25

Following onto the end of my above comment, here's more about MD5 vs SHA performance:

In addition to the wider input blocks for the SHA family, MD5 doesn't have dedicated instructions in modern CPUs like the SHA family do, so MD5, while ostensibly "simpler," is done in software, for things that run on the CPU. In fact, one of the goals of the SHA2 suite and up (anything not SHA1) was performance. MD5 was designed when we still thought making the hash algorithm slower was a useful property when trying to make things more secure (another relic of that time, 3DES, was born of the same thought process and is literally just doing DES 3 times on each block).

Now we know that the speed of the hash itself is not directly proportional to nor consistently relevant to how secure it is, for numerous reasons, and that other properties of the algorithm and its output are more important and more durable in the face of advancing technology.

3

u/arpan3t Dec 08 '25

That’s great, now do MD5 because that’s what I suggested, not SHA1. You actually just made up an argument and are… arguing with yourself.

1

u/Sea_Ice6776 Dec 09 '25

Done yesterday.

Feel free to retract the statement...

1

u/PhysicalPinkOrchid Dec 09 '25

He won't... he has a fragile ego.