简体   繁体   English

使用Ruby的md5复制PHP 5中的md5 raw_output标志(原始字节)

[英]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. 由于荒谬的SOAP身份验证方案,我需要md5使用其他一些参数来散列API密钥。 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). 不幸的是,提供的唯一示例代码是用PHP编写的,由于我觉得不可思议的原因,它要求md5哈希在PHP中使用可选的raw_output标志( http://php.net/manual/en/function.md5.php )这导致它返回二进制(然后我必须base64编码)。

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. 我的应用程序是用Ruby编写的,如果我不需要,我不想将这部分推迟到PHP文件中。 However, I can't seem to find out how to get Ruby to return the hash in binary. 但是,我似乎无法找到如何让Ruby以二进制形式返回哈希值。 When I hash it normally in PHP, the output matches my Ruby output, but that's not what they're asking for. 当我在PHP中正常散列它时,输出与我的Ruby输出匹配,但这不是他们要求的。

PHP: 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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