Rails and PHP encode64 not agreeing -


i'm try create hmac in rails , verify in php.
rails shell:

       pry(main)>appsecret = '00916893840fe0a29dfdc261efd3a26a&'                   pry(main)>openssl::hmac.hexdigest('sha1', appsecret, 'get&http%3a%2f%2fopen.tianya.cn%2foauth%2frequest_token.php&oauth_consumer_key%3dfc69b18eb12bab1e9b35d1093c4de9290516cfdc4%26oauth_nonce%3dc09e4bf167fbc7eb374b1abb02b5268d%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1366882036%26oauth_version%3d1.0')        => "8494f6237ee6042a3da8848db21284be17bf6ade" 

php:

      $appsecret = '00916893840fe0a29dfdc261efd3a26a&';       $signature = base64_encode(hash_hmac('sha1', $appsecret, 'get&http%3a%2f%2fopen.tianya.cn%2foauth%2frequest_token.php&oauth_consumer_key%3dfc69b18eb12bab1e9b35d1093c4de9290516cfdc4%26oauth_nonce%3dc09e4bf167fbc7eb374b1abb02b5268d%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1366882036%26oauth_version%3d1.0',true));       var_dump($signature); 

rails result:8494f6237ee6042a3da8848db21284be17bf6ade

php result:ayw4/l22fctxpvppgay/ud8yhmu=
any great appreciated!

you have 2 problems:

  1. your ruby output returned ascii hex, not raw, , not base64 encoding it
  2. you have argument order php hash_hmac() function wrong.

ruby:

appsecret = '00916893840fe0a29dfdc261efd3a26a&' data = 'get&http%3a%2f%2fopen.tianya.cn%2foauth%2frequest_token.php&oauth_consumer_key%3dfc69b18eb12bab1e9b35d1093c4de9290516cfdc4%26oauth_nonce%3dc09e4bf167fbc7eb374b1abb02b5268d%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1366882036%26oauth_version%3d1.0'  digest = openssl::hmac.digest('sha1', appsecret, data) base64.encode64(digest) 

php:

$appsecret = '00916893840fe0a29dfdc261efd3a26a&'; $data = 'get&http%3a%2f%2fopen.tianya.cn%2foauth%2frequest_token.php&oauth_consumer_key%3dfc69b18eb12bab1e9b35d1093c4de9290516cfdc4%26oauth_nonce%3dc09e4bf167fbc7eb374b1abb02b5268d%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1366882036%26oauth_version%3d1.0';  $digest = hash_hmac('sha1', $data, $appsecret, true); echo base64_encode($digest); 

both produce hjt2i37mbco9qisnshkevhe/at4=. note, however, ruby output has trailing new-line, need normalize (either remove in ruby or add 1 in php) if want compare them directly.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -