Created
April 28, 2023 20:50
-
-
Save badmotorfinger/6026340849f49562cbe0f5f55befd6f2 to your computer and use it in GitHub Desktop.
Generates a single hash from the contents of a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the directory to hash | |
$dir = "C:\Path\To\Directory" | |
# Define the hash algorithm to use | |
$hashAlgorithm = "SHA256" | |
# Get all files in the directory (including subdirectories) | |
$files = Get-ChildItem -Path $dir -Recurse | Where-Object {!$_.PSIsContainer} | |
# Create an empty hash object | |
$hashObject = [System.Security.Cryptography.HashAlgorithm]::Create($hashAlgorithm) | |
# Calculate the hash of each file and add it to the hash object | |
foreach ($file in $files) { | |
$stream = [System.IO.File]::OpenRead($file.FullName) | |
$hash = $hashObject.ComputeHash($stream) | |
$stream.Close() | |
$hashString = [System.BitConverter]::ToString($hash).Replace("-", "").ToLower() | |
$hashObject.Initialize() | |
$hashObject.TransformBlock([System.Text.Encoding]::UTF8.GetBytes($hashString), 0, $hashString.Length, $hashString, 0) | |
} | |
# Finalize the hash and convert it to a string | |
$hashObject.TransformFinalBlock([System.Array]::Empty(), 0, 0) | |
$grantTotalHash = [System.BitConverter]::ToString($hashObject.Hash).Replace("-", "").ToLower() | |
# Output the grant-total hash | |
Write-Output "Grant-total hash: $grantTotalHash" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment