Skip to content

Instantly share code, notes, and snippets.

@xpando
Last active November 23, 2015 18:58
Show Gist options
  • Select an option

  • Save xpando/6ebcfc4d1ba0dd04b98a to your computer and use it in GitHub Desktop.

Select an option

Save xpando/6ebcfc4d1ba0dd04b98a to your computer and use it in GitHub Desktop.
PowerShell script to hash files. Can accept pipeline input to hash multiple files into one hash. e,g, dir *.dll | Hash.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True,ValueFromPipeline=$True,Position=0)]
[Alias("PSPath","FullName")]
[String[]]$Path,
[int]$BufferSize = 65535
)
Begin {
$buffer = New-Object byte[] $BufferSize
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
}
Process {
$fs = [IO.File]::OpenRead($Path)
try {
$size = $fs.Read($buffer, 0, $BufferSize)
while ($size -eq $BufferSize)
{
$md5.TransformBlock($buffer, 0, $size, $buffer, 0) | out-null
$size = $fs.Read($buffer, 0, $BufferSize)
}
$md5.TransformBlock($buffer, 0, $size, $buffer, 0) | out-null
}
finally {
$fs.Dispose() | out-null
}
}
End {
$md5.TransformFinalBlock((New-Object byte[] 0), 0, 0);
$hash = [BitConverter]::ToString($md5.Hash).Replace("-", "").ToLower();
$md5.Dispose()
$hash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment