views:

171

answers:

1

Due to an absurd SOAP authentication scheme I need to md5 hash an API key with some other parameters. Unfortunately the only sample code provided is written in PHP and, for reasons I find unfathomable, it requires that the md5 hashing use the optional raw_output flag in PHP (http://php.net/manual/en/function.md5.php) which causes it to return binary (which I then have to base64 encode).

My app is written in Ruby and I don't want to defer this portion to a PHP file if I don't have to. However, I can't seem to find out how to get Ruby to return the hash in binary. When I hash it normally in PHP, the output matches my Ruby output, but that's not what they're asking for.

PHP:

<?php
  $encode = "test";
  echo md5($encode); // 098f6bcd4621d373cade4e832627b4f6
  echo "\n";
  // PHP5 - md5 with raw_output flag set to true - what I need to mimic in Ruby
  echo md5($encode, true); // binary that looks something like: ?k?F!?s??N?&'??
  echo "\n";
?>

Ruby:

require 'digest/md5'
encode = "test"
puts Digest::MD5.hexdigest(encode) # 098f6bcd4621d373cade4e832627b4f6

Any assistance is appreciated.

+4  A: 

Just use digest instead of hexdigest:

puts Digest::MD5.digest(encode) 
Brian McKenna
Thanks Brian, geez, I have no idea why it was so hard for me to figure that out!
adriandz