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