Thursday 8 January 2009

Get-Hash.psm1

  1. function Get-Hash { 
  2. <# 
  3. .SYNOPSIS 
  4.    Creates a Hash of a file and prints the hash   
  5. .DESCRIPTION 
  6.     Uses System.Security.Cryptography.HashAlgorithm and members to create the hash 
  7.     Also uses System.Text.AsciiEncoding to convert string to byte array. 
  8.     Created as a Module. 
  9. .NOTES 
  10.     File Name  : Get-Hash.PSM1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13.     Thanks to the #PowerShell Twitter Posse for help figuring out -verbose. 
  14.     http://www.pshscripts.blogspot.com 
  15.     Based on  
  16.     http://tinyurl.com/aycszb written by Bart De Smet 
  17. .PARAMETER Algorithm 
  18.     The name of one of the hash Algorithms defined at 
  19.     http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx 
  20. .PARAMETER File 
  21.     The name of a file to provide a hash for
  22. .EXAMPLE 
  23.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| Get-Hash sha1 -verbose:$true 
  24.     OK - I'll be chatty 
  25.  
  26.     Processing C:\foo\asciifile.txt 
  27.     sha1 hash of file C:\foo\asciifile.txt is "3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5" 
  28.  
  29.     Processing C:\foo\log.txt 
  30.     sha1 hash of file C:\foo\log.txt is "89dd3b94d3cb7f645498925e77346aa9218d7ffe" 
  31.  
  32.     Processing C:\foo\sites.txt 
  33.     sha1 hash of file C:\foo\sites.txt is "2e434f1c2c16fc1060cd9f2e0226e142ea450ce4" 
  34.  
  35.     Processing C:\foo\test.txt 
  36.     File C:\foo\test.txt can not be hashed 
  37.  
  38.     Processing C:\foo\test.txt.txt 
  39.     File C:\foo\test.txt.txt can not be hashed 
  40.  
  41.     Processing C:\foo\test2.txt 
  42.     File C:\foo\test2.txt can not be hashed 
  43.  
  44.     Processing C:\foo\unicodefile.txt 
  45.     sha1 hash of file C:\foo\unicodefile.txt is "3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5" 
  46. .EXAMPLE 
  47.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| Get-Hash sha1 
  48.     3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5 
  49.     89dd3b94d3cb7f645498925e77346aa9218d7ffe 
  50.     2e434f1c2c16fc1060cd9f2e0226e142ea450ce4 
  51.     3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5 
  52. .EXAMPLE 
  53.     PS C:\foo> Get-Hash  md5 asciifile.txt -verbose:$true 
  54.     OK - I'll be chatty 
  55.  
  56.     Processing asciifile.txt 
  57.     md5 hash of file asciifile.txt is "b5fe789f9d497f8022d47f620de25499" 
  58. .EXAMPLE 
  59.     PS C:\foo> Get-Hash  md5 asciifile.txt 
  60.     b5fe789f9d497f8022d47f620de25499 
  61. #> 
  62. [CmdletBinding()] 
  63. param ( 
  64. [Parameter(Position=0, mandatory=$true)] 
  65. [string] $Algorithm, 
  66. [Parameter(Position=1, mandatory=$true, valuefrompipeline=$true)] 
  67. [string] $File 
  68.  
  69. begin   { 
  70. if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true
  71. if ($verbose) {"OK - I'll be chatty";""
  72. process { 
  73.     if ($verbose) {"Processing $file"
  74.     $Algo=[System.Security.Cryptography.HashAlgorithm]::Create($algorithm) 
  75.     if ($Algo){ 
  76.         $Fc = Get-Content $File 
  77.         if ($fc.count -gt 0) { 
  78.             $Encoding = New-Object System.Text.ASCIIEncoding 
  79.             $Bytes = $Encoding.GetBytes($fc)     
  80.             # Now compute hash 
  81.             $Hash = $Algo.ComputeHash($bytes)    
  82.             $Hashstring ="" 
  83.             foreach ($byte in $hash) {$hashstring += $byte.tostring("x2")} 
  84.             # pass hash string on 
  85.             if ($verbose){ 
  86.               "{0} hash of file {1} is `"{2}`"" -f $algorithm, $file, $hashstring 
  87.               "" 
  88.             } 
  89.             else
  90.              $Hashstring 
  91.             } 
  92.         } 
  93.         else
  94.              if ($verbose) {"File {0} can not be hashed" -f $file ; ""}      
  95.         }} 
  96.     else
  97.     "Algorithm {0} not found" -f $algorithm 
  98.     } 
  99. } # end process block 
  100. } # End Function Definition 
  101. Export-ModuleMember Get-hash 

Share this post :

No comments: