Ruby and PHP HMACs not agreeing

点点圈 提交于 2019-12-03 02:26:01

ruby's OpenSSL::HMAC.hexdigest expects first key and then message.

irb(main):002:0> OpenSSL::HMAC.hexdigest('sha256','3D2143BD-6F86-449F-992C-65ADC97B968B','A522EBF2-5083-484D-99D9-AA97CE49FC6C,1234567890,/api/comic/aWh62,GET')
=> "e5f6995ba1496b2fb144329b2d1b3b23c8fa3211486e57bfaec5d993a1da9d15"

I noticed that

hash = HMAC::SHA256(key) 
hash << a
hash << b
hash << c

gives different result than PHP's

hash_hmac('sha256',$a.$b.$c, $key)

beware of this caveat. To get correct, just do

hash = HMAC::SHA256(key)
hash << "#{a}#{b}#{c}"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!