-
Notifications
You must be signed in to change notification settings - Fork 63
/
GetFileHash.ps1
37 lines (33 loc) · 1.04 KB
/
GetFileHash.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Get-FileHash has been introduced only in PowerShell 4.0
# Here's a compatible implementation for previous versions (SHA1 only for now)
function Get-FileHash {
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[string]$Path,
[string]$Algorithm = "SHA1"
)
process
{
if ($Algorithm -ne "SHA1") {
throw "Unsupported algorithm: $Algorithm"
}
$fullPath = Resolve-Path $Path
$f = [System.IO.File]::OpenRead($fullPath)
$sham = $null
try {
$sham = new-object System.Security.Cryptography.SHA1Managed
$hash = $sham.ComputeHash($f)
$hashSB = new-object System.Text.StringBuilder -ArgumentList ($hash.Length * 2)
foreach ($b in $hash) {
$sb = $hashSB.AppendFormat("{0:x2}", $b)
}
return [pscustomobject]@{Algorithm="SHA1"; Path=$fullPath; Hash=$hashSB.ToString()}
}
finally {
$f.Close()
if($sham) { $sham.Clear() }
}
}
}