Skip to content

Instantly share code, notes, and snippets.

@badmotorfinger
Created April 28, 2023 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badmotorfinger/6026340849f49562cbe0f5f55befd6f2 to your computer and use it in GitHub Desktop.
Save badmotorfinger/6026340849f49562cbe0f5f55befd6f2 to your computer and use it in GitHub Desktop.
Generates a single hash from the contents of a directory
# 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