Topics


Blogs


Forums


Samples


Media


Labs


Resources

 




DevCentral > Weblogs > Joe Pruitt - A Software Architect's take on Network Security
 Unix To PowerShell - Md5
posted on Monday, May 18, 2009 8:08 AM

PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on Unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

md5

The Unix “md5” command (aliased to “openssl dgst –md5” or “md5sum”) calculates and verifies 128-bit MD5 hashes as described in RFC 1321.  The MD5 hash (or checksum) functions as a compact digital fingerprint of a file.  It is extremely unlikely that any two non-identical files existing in the real world will have the same MD5 hash.

The “openssl dgst –md5” option, there is a “-c” option to include colons between the two digit groups in the digest.  The “md5sum” command does not.  I’ve opted to use the output format of the “openssl” version of the command for my PowerShell function.

Unix PowerShell Description
-c -colons Print the digest in two-digit groups
separated by colons.

 

   1: #----------------------------------------------------------------
   2: # Md5.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,
   7:   [bool]$colons = $false
   8: );
   9:  
  10: #----------------------------------------------------------------
  11: # function Do-Md5
  12: #----------------------------------------------------------------
  13: function Do-Md5()
  14: {
  15:   param
  16:   (
  17:     [string]$filespec = $null,
  18:     [bool]$colons = $false
  19:   );
  20:   
  21:   $hashAlgorithm = new-object -TypeName "System.Security.Cryptography.MD5CryptoServiceProvider";
  22:   
  23:   $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue);
  24:   foreach ($file in $files)
  25:   {
  26:     $stream = $file.OpenRead();
  27:     $hashByteArray = $hashAlgorithm.ComputeHash($stream);
  28:     $stream.Close();
  29:     
  30:     $md5StringBuilder = New-Object System.Text.StringBuilder
  31:     
  32:     $hashByteArray | % {
  33:       if ( $colons )
  34:       {
  35:         if ( $md5StringBuilder.Length -gt 0 ) { [void] $md5StringBuilder.Append(":") }
  36:       }
  37:       [void] $md5StringBuilder.Append($_.ToString("x2"))
  38:     }
  39:     
  40:     "MD5($($file.Name))= $($md5StringBuilder.ToString())";
  41:  
  42:     # Ensure stream is closed if exceptions occurred.
  43:     trap
  44:     {
  45:         if ($stream -ne $null) { $stream.Close(); }
  46:         break;
  47:     }
  48:   }
  49: }
  50:  
  51: Do-Md5 -filespec $filespec -colons $colons;

You can download the source for this script here: Md5.ps1



 
      

Feedback


5/19/2009 1:59 AM
Gravatar PowerShell already uses some Unix commands as aliases e.g. ls is alias for Get-ChildItem. Joe Pruitt
Richard Siddaway's Blog

7/29/2009 8:13 PM
Gravatar really nice article.I am a newbie in ruby
technology forum

10/7/2009 5:08 PM
Gravatar Thanks for the code, think I am learning more ruby, very slowly though!
Website Design
 Leave Feedback
Title  
Name  
Email
Url
Comments   
Please add 6 and 8 and type the answer here: