Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # code from http://aperturescience.su
- 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 either 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)] [ValidateScript({Test-Path $_ -PathType 'lea'})] [String] $File,
- [ValidateSet("sha1", "sha256", "sha384", "sha512", "md5", "ripemd160")] $HashAlgorithm = "sha256",
- [switch] $Group
- )
- Process
- {
- #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 "-","")
- }
- }
- }
- # code from http://aperturescience.su
Advertisement
Add Comment
Please, Sign In to add comment