Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

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
6/25/2010 10:38 PM
Gravatar This is such a great resource that you are providing and you give it away for free. I love seeing websites that
understand the value of providing a quality resource for free. Thanks for this wonderful resource!
deutsches Bingo

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 8 and 2 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or