kjacobsen icon

get-FileHash

kjacobsen | PRO | 11/15/12 02:25:04 AM UTC | 0 ⭐ | 533 👁️ | Never ⏰ | []
PowerShell |

3.09 KB

|

None

|

0 👍

/

0 👎

#
# Enum HashFunction
#       Contains possible hash functions to choose from
#
 
Add-Type -TypeDefinition @"
    public enum HashFunction
    {
        sha1,
        sha256,
        sha384,
        sha512,
        md5,
        ripemd160
    }
"@
 
Function Get-FileHash
{
<#
.SYNOPSIS
Gets a hash value for the file specified
 
.DESCRIPTION
With this function you can calculate the hash of the contents of a specified file. Hash algorithms supported include SHA1, SHA2 256/348/512, MD5 and RIPEMD160
 
.PARAMETER File
[PIPELINE] File to get hash of.
 
.PARAMETER HashAlgorith
[OPTIONAL] The hash algorithm to use, must be of of enum HashFunction, options include sha1, sha256, sha384, sha512, md5 or ripemd160
 
.PARAMETER Group
[SWITCH] Group by bytes, by default no grouping and hash returned as single long string. If specified byptes separated by "-"
 
.INPUTS
Accepts strings of paths to files in Pipeline
 
.OUTPUTS
hash of file
 
.EXAMPLE
get-FileHash c:\myfile.txt
Get hash (SHA256) of file, c:\myfile.txt 
 
.EXAMPLE
get-FileHash c:\myfile.txt -HashAlgorithm md5
Get md5 hash of file, c:\myfile.txt
 
.EXAMPLE
dir c:\afolder | foreach { $_.fullname} | get-FileHash
get (SHA256) hashes for all files in c:\afolder
 
.NOTES
NAME: Get-FileHash
LASTEDIT: 2012-11-15 11:15:00
KEYWORDS:
 
.LINK
https://blogs.technet.com/b/msrc/archive/2012/11/13/verifying-update-hashes.aspx?Redirected=true
 
.LINK
http://blogs.msdn.com/b/mwilbur/archive/2007/03/14/get-sha256.aspx
 
.LINK
http://aperturescience.su/
#>
[CMDLetBinding()]
param
(
  [Parameter(mandatory=$true, valuefrompipeline=$true)] [String] $File,
  [HashFunction] $HashAlgorithm = "sha256",
  [switch] $Group
)
 
Process
{
    #check file exists, otherwise throw error
    if (! (Test-Path $file))
    {
        throw "Could not find file $file"
    }
    
    #variable to store hash of the file
    $hash
    
    #select the correct hash function
    switch ($HashAlgorithm)
    {
        "sha1"
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha1]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }
        "sha256" 
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha256]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }       
        "sha384"
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha384]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }       
        "sha512"
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha512]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }       
        "md5" 
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.md5]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }
        "ripemd160" 
            {
                $hash = [system.bitconverter]::tostring([System.Security.Cryptography.ripemd160]::create().computehash([system.io.file]::openread((resolve-path $file))))
            }
    }
    
    
    
    #leave the default grouping of byte pairs?
    if ($Group)
    {
        return $hash
    }
    else
    {
        return ($hash -replace "-","")
    }
}
 
}

Comments