Showing posts with label System.Security.Cryptography.Sha1CryptoServiceProvider. Show all posts
Showing posts with label System.Security.Cryptography.Sha1CryptoServiceProvider. Show all posts

Monday, 16 August 2010

Get-Sha1HashFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the SHA1 Crypto Provider to hash a file 
  4. .DESCRIPTION 
  5.     This script uses SHA1 Cryptoprovider to hash a file and print the hash. A second 
  6.     hash is created of the same file, but with a space added. The file I picked is a 
  7.     large text file I keep in c:\foo for uses like this! 
  8. .NOTES 
  9.     File Name  : Get-Sha1HashFile.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     Posted to TechEd Sample Gallery at: 
  16.  
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-Sha1HashFile.ps1 
  19.     Hashing     : C:\foo\gd.txt 
  20.     Results in  : 760bb67356560232 
  21.     Hashing     :  C:\foo\gd.txt + space 
  22.     Results in  : 4f7b4902168bba7d 
  23.      
  24. #> 
  25.  
  26. # Create Input Data 
  27. $file       = (ls c:\foo\*.txt | sort -descending length)[0] # get largest file 
  28. $filestr    = Get-Content $file 
  29. $filebytes1 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files
  30. $filebytes2 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files+" "
  31.  
  32. # Create a New SHA1 Crypto Provider 
  33. $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
  34.  
  35. # Now hash that 
  36. $result1 = $sha.ComputeHash($filebytes1
  37. "Hashing     : {0}" -f $file.FullName 
  38. "Results in  : {0}" -f [system.BitConverter]::ToUint64($result1,0).tostring("x"
  39.  
  40. $result2 = $sha.ComputeHash($filebytes2
  41. "Hashing     : {0}" -f " $($file.FullName) + space" 
  42. "Results in  : {0}" -f [system.BitConverter]::ToUint64($result2,0).tostring("x"

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