Last active
November 23, 2015 18:58
-
-
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
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
| [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