简体   繁体   中英

ruby openssl hmac php equivalent

I have to convert this ruby code into PHP equivalent.

[OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, policy)].pack("m").strip

I converted it into the following code but I'm not sure if it is correct or not.

$s = hash_hmac('sha1', $secret_key, $policy);
$s = base64_encode($sig);
$s = trim($sig);
return $s;

I'm not sure if I need to set the last parameter of hash_hmac to true:

$sig = hash_hmac('sha1', $secret_key, $policy, true);

or none is correct and should do this in a different way.

Which would be the correct equivalent?

Ok, I have just installed ruby myself to test. I have tested and the equivalent is:

$sig = hash_hmac('sha1', $policy, $secret_key, true);
$sig = base64_encode($sig);
$sig = trim($sig);
return $sig;

The order of parameters is inverted in php, first data, then key. And the raw_output parameter needs to be true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM