duanou3868 2015-12-10 13:57
浏览 93
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douyan6742 2015-12-10 14:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?