Created
May 23, 2015 14:38
-
-
Save rufflabs/bb1623f3819b31be32d6 to your computer and use it in GitHub Desktop.
Gets the hash of a file, useful for versions of PowerShell before the built in commandlet.
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
function Get-Hash { | |
param( | |
[Parameter(Position=0,Mandatory=$true)]$Path | |
) | |
# Load crypto library if needed | |
if(([AppDomain]::CurrentDomain.GetAssemblies() | ? {$_ -Match 'System.Security'}) -eq $Null) { | |
[Reflection.Assembly]::LoadWithPartialName("System.Security") | Out-Null | |
} | |
$Sha1 = New-Object System.Security.Cryptography.Sha1Managed | |
if($Path -ne $Null) { | |
$FileHandler = [System.IO.File]::Open($Path, 'open', 'read') | |
$Hash = $Sha1.ComputeHash($FileHandler) | % {$_.ToString('x2')} | |
$FileHandler.Dispose() | |
return (-Join $Hash) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment