Friday 13 August 2010

Get-Sha1Hash.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the SHA1 Crypto Provider to hash a string 
  4. .DESCRIPTION 
  5.     This script creates 2 strings, and a SHA1 Crypto Provider. 
  6.     The script then hashes the strings and displays the results.  
  7. .NOTES 
  8.     File Name  : Get-Sha1Hash.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1cryptoserviceprovider.aspx 
  16.     Posted to TechEd Sample Gallery at: 
  17.      
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Security.Crpytography\Get-Sha1Hash.ps1' 
  20.     Hashing     : This is a string to hash 
  21.     Results in  : z→???_r♠W??o???]rv?? 
  22.   
  23.     Hashing     : this is a string to hash 
  24.     Results in  : ???☺$?Z??♀???????U?' 
  25. #> 
  26.  
  27. # Create Input Data 
  28. $enc = [system.Text.Encoding]::ASCII 
  29. $string1 = "This is a string to hash" 
  30. $string2 = "this is a string to hash" 
  31. $data1 = $enc.GetBytes($string1
  32. $data2 = $enc.GetBytes($string2
  33.  
  34. # Create a New SHA1 Crypto Provider 
  35. $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
  36.  
  37. # Now hash and display results 
  38. $result1 = $sha.ComputeHash($data1
  39. "Hashing     : {0}" -f $string1 
  40. "Results in  : {0}" -f $enc.Getstring($result1
  41. "" 
  42.  
  43. $result2 = $sha.ComputeHash($data2
  44. "Hashing     : {0}" -f $string2 
  45. "Results in  : {0}" -f $enc.Getstring($result2

No comments: