Ruby and PHP HMACs not agreeing

前端 未结 2 1352
无人共我
无人共我 2021-02-05 20:35

I\'m trying to create an HMAC in Ruby and then verify it in PHP.

Ruby:

require \'openssl\'
message = \"A522EBF2-5083-484D-99D9-AA97CE49FC6C,1234567890,/a         


        
2条回答
  •  暖寄归人
    2021-02-05 21:01

    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}"
    

提交回复
热议问题