document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #
  2. # Enum HashFunction
  3. #       Contains possible hash functions to choose from
  4. #
  5.  
  6. Add-Type -TypeDefinition @"
  7.     public enum HashFunction
  8.     {
  9.         sha1,
  10.         sha256,
  11.         sha384,
  12.         sha512,
  13.         md5,
  14.         ripemd160
  15.     }
  16. "@
  17.  
  18. Function Get-FileHash
  19. {
  20. <#
  21. .SYNOPSIS
  22. Gets a hash value for the file specified
  23.  
  24. .DESCRIPTION
  25. 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
  26.  
  27. .PARAMETER File
  28. [PIPELINE] File to get hash of.
  29.  
  30. .PARAMETER HashAlgorith
  31. [OPTIONAL] The hash algorithm to use, must be of of enum HashFunction, options include sha1, sha256, sha384, sha512, md5 or ripemd160
  32.  
  33. .PARAMETER Group
  34. [SWITCH] Group by bytes, by default no grouping and hash returned as single long string. If specified byptes separated by "-"
  35.  
  36. .INPUTS
  37. Accepts strings of paths to files in Pipeline
  38.  
  39. .OUTPUTS
  40. hash of file
  41.  
  42. .EXAMPLE
  43. get-FileHash c:\\myfile.txt
  44. Get hash (SHA256) of file, c:\\myfile.txt
  45.  
  46. .EXAMPLE
  47. get-FileHash c:\\myfile.txt -HashAlgorithm md5
  48. Get md5 hash of file, c:\\myfile.txt
  49.  
  50. .EXAMPLE
  51. dir c:\\afolder | foreach { $_.fullname} | get-FileHash
  52. get (SHA256) hashes for all files in c:\\afolder
  53.  
  54. .NOTES
  55. NAME: Get-FileHash
  56. LASTEDIT: 2012-11-15 11:15:00
  57. KEYWORDS:
  58.  
  59. .LINK
  60. https://blogs.technet.com/b/msrc/archive/2012/11/13/verifying-update-hashes.aspx?Redirected=true
  61.  
  62. .LINK
  63. http://blogs.msdn.com/b/mwilbur/archive/2007/03/14/get-sha256.aspx
  64.  
  65. .LINK
  66. http://aperturescience.su/
  67. #>
  68. [CMDLetBinding()]
  69. param
  70. (
  71.   [Parameter(mandatory=$true, valuefrompipeline=$true)] [String] $File,
  72.   [HashFunction] $HashAlgorithm = "sha256",
  73.   [switch] $Group
  74. )
  75.  
  76. Process
  77. {
  78.     #check file exists, otherwise throw error
  79.     if (! (Test-Path $file))
  80.     {
  81.         throw "Could not find file $file"
  82.     }
  83.    
  84.     #variable to store hash of the file
  85.     $hash
  86.    
  87.     #select the correct hash function
  88.     switch ($HashAlgorithm)
  89.     {
  90.         "sha1"
  91.             {
  92.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha1]::create().computehash([system.io.file]::openread((resolve-path $file))))
  93.             }
  94.         "sha256"
  95.             {
  96.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha256]::create().computehash([system.io.file]::openread((resolve-path $file))))
  97.             }      
  98.         "sha384"
  99.             {
  100.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha384]::create().computehash([system.io.file]::openread((resolve-path $file))))
  101.             }      
  102.         "sha512"
  103.             {
  104.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.sha512]::create().computehash([system.io.file]::openread((resolve-path $file))))
  105.             }      
  106.         "md5"
  107.             {
  108.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.md5]::create().computehash([system.io.file]::openread((resolve-path $file))))
  109.             }
  110.         "ripemd160"
  111.             {
  112.                 $hash = [system.bitconverter]::tostring([System.Security.Cryptography.ripemd160]::create().computehash([system.io.file]::openread((resolve-path $file))))
  113.             }
  114.     }
  115.    
  116.    
  117.    
  118.     #leave the default grouping of byte pairs?
  119.     if ($Group)
  120.     {
  121.         return $hash
  122.     }
  123.     else
  124.     {
  125.         return ($hash -replace "-","")
  126.     }
  127. }
  128.  
  129. }
');