Question

I used to generate digital signature in Ruby like so:

def generate_signature
  data = "Databeingpassed"
  secret_key = "Somesecretkey" 
  digest = OpenSSL::Digest::Digest.new('sha256')
  Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, data)).strip
end

Output: 2XBK7UXA9oDsfJj1TBE2maptpB6i1MJ4uadY1AXlQOQ=

How to achieve the same with Php, I'm using the following code in php, but the outputs are different

function generate_signature() {
  $data = "Databeingpassed"
  $secret_key = "Somesecretkey" 

  $message = hash('sha256', $secret_key, $data);
  return base64_encode($message);
}

Output: hgy/KMG3zTSgjUrzA/3nNvN+vApna6A7JqtZwx+r9Ng=

Any ideas?

Was it helpful?

Solution

I've found a way, in case if someone will need:

function generate_signature() {
  $data = "Databeingpassed"
  $secret_key = "Somesecretkey" 

  return base64_encode(hash_hmac('sha256', $data, $secret_key, true));
}

The output:

2XBK7UXA9oDsfJj1TBE2maptpB6i1MJ4uadY1AXlQOQ=
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top