将PHP中的hash_hmac转换为rails等效项

我想将PHP哈希代码转换为等效的ruby或rails代码。 </ p>

  $ publicHash ='3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348'; 
$ privateHash ='e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e';
$ content = json_encode(array('test'=&gt;'content')) ;
$ hash = hash_hmac('sha256',$ content,$ privateHash);
</ code> </ pre>

$ content是request params hash。 </ p>

我使用,</ P>

 <代码> public_hash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348' 
private_hash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e'
content = JSON ::编码转换在ROR( paramsString)
hash = OpenSSL :: HMAC.hexdigest(OpenSSL :: Digest.new('sha256'),private_hash,content)
</ code> </ pre>

但没有运气。 我缺少什么?</ p>
</ div>

展开原文

原文

I want to convert below PHP hash-code to equivalent ruby or rails code.

$publicHash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348';
$privateHash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e';
$content = json_encode( array( 'test' => 'content' ) );
$hash = hash_hmac('sha256', $content, $privateHash);

$content is request params hash.

I converted in ROR using,

public_hash =  '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348'
private_hash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e'
content = JSON::encode( paramsString )
hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), private_hash, content)

But no luck. Anything I am missing?

douhui4699
douhui4699 我们不是来做你的工作。您尝试了什么以及遇到了什么问题?
4 年多之前 回复

1个回答



你可以这样做</ p>

  require'openssl'
require'json'

private_hash ='e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e'
content = {“test”=&gt; “content”} .to_json
digest = OpenSSL :: Digest.new('sha256')

hash = OpenSSL :: HMAC.hexdigest(摘要,private_hash,内容)
</ code> </ pre>

有关详细信息,请参阅 http://ruby-doc.org/stdlib-2.2.3/libdoc/openssl/rdoc/OpenSSL/HMAC.html </ p>
</ div>

展开原文

原文

You can do like this

require 'openssl'
require 'json'

private_hash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e'
content = { "test" => "content" }.to_json
digest = OpenSSL::Digest.new('sha256')

hash = OpenSSL::HMAC.hexdigest(digest, private_hash, content)

For more information, please see http://ruby-doc.org/stdlib-2.2.3/libdoc/openssl/rdoc/OpenSSL/HMAC.html

dpx49470
dpx49470 我想你打算实现API身份验证。 当然,附加在标题中仍然是一个不错的选择
4 年多之前 回复
dsj0312
dsj0312 现在如何在请求中附加这个? 我可以像在PHP中那样追加标题,还是有其他方法?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐