简体   繁体   中英

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.

只需使用digest而不是hexdigest

puts Digest::MD5.digest(encode) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM