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"

No comments: