Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

duplicating the md5 raw_output flag (raw bytes) in PHP 5 with Ruby's md5

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.

like image 326
adriandz Avatar asked Jan 23 '23 19:01

adriandz


1 Answers

Just use digest instead of hexdigest:

puts Digest::MD5.digest(encode) 
like image 71
Brian McKenna Avatar answered Jan 25 '23 09:01

Brian McKenna