I should have known - tasks like this are well suited for script/dynamic languages. So today I give you MSDN hash validator in Ruby:

require 'digest/sha1'
if ARGV.length != 2 then puts "Usage: msdnh file_to_hash hash_as_hex"
else 
  hasher = Digest::SHA1.new 
  File.open(ARGV[0], "rb") { |f| hasher << f.read(1024*1024) while !f.eof? }
  if ARGV[1].downcase != hasher.hexdigest then puts "Hashes do NOT match, file is invalid."
  else puts "Hashes match, file is valid."
  end
end

It's not much shorter than C# version - main savings are that there is less "usings" (called "require" in Ruby) and the fact that SHA1 hasher can spit out hex string of the hash directly. C# version saves on not having to load bytes and stuff them in the hasher but Ruby stays one-liner here thanks to it's blocks, iterators and "while" modifier.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
0 Comments