Skip to content

Instantly share code, notes, and snippets.

@rufflabs
Created May 23, 2015 14:38
Show Gist options
  • Save rufflabs/bb1623f3819b31be32d6 to your computer and use it in GitHub Desktop.
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.
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