Ruby HMAC签名问题 [英] Ruby HMAC signing issue

查看:118
本文介绍了Ruby HMAC签名问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了HMAC的问题. 我必须先签署表格,然后再将其发送到银行. 他们仅在其文档中提供了PHP中的示例.

I got an issue with HMAC. I have to sign a form before sending it to a bank. They only provide an example in PHP in their documentation.

我有一个十六进制密钥来签名我的数据(例如FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A).

I have a hex key to sign my data (e.g. FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A).

在PHP示例中,他们在对数据签名之前使用密钥执行此操作:

In the PHP exemple, they do this with the key before signing the data:

 $key = "FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A";
 $message = "param1=a&param2=b";

 $binKey = pack('H*', $key);
 $signature = hash_hmac('sha512', $msg, $binKey);
 echo $signature;

 // => a3efb70368bee502ea57a1a4708cac8912a5172075ea8dec2de2770dfbb4c8fb587f03fdadc0ca4f9e1bb024cfda12866295b259f5fb4df2fe14d960874a68ab

我不明白他们为什么要打包钥匙,以及我是否应该对钥匙做类似的事情. 我在Ruby代码中做了以下操作:

I don't understand why they pack the key and if I should do something similar with my key. I did the following in my Ruby code:

key = "FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A"
message = "param1=a&param2=b"

digest = OpenSSL::Digest.new('sha512')
signature = OpenSSL::HMAC.hexdigest(digest, key, message)

puts signature

# => d817611845246640d1224a0874bf60fed0956a367aa3069b7947cbec56903bb5d8c54df170f5504c586dad55e4f879c70cf1a40526cfc9f35411195822c535ed

推荐答案

您需要在Ruby中执行此操作:

You need to do this in Ruby:

hash = OpenSSL::HMAC.hexdigest(digest, [key].pack('H*'), message)

这里的真正问题是您的PHP代码使用两个变量名来表示消息.设置$message,然后使用$msg,这意味着您正在计算未定义变量的哈希值.

The real issue here is that your PHP code uses two variable names for the message. You set $message, then use $msg, which means you're computing the hash for an undefined variable.

这篇关于Ruby HMAC签名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆