0

我曾经像这样在 Ruby 中生成数字签名:

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=

如何用 Php 实现相同的,我在 php 中使用以下代码,但输出不同

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

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

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

有任何想法吗?

4

1 回答 1

0

我找到了一种方法,以防有人需要:

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

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

输出:

2XBK7UXA9oDsfJj1TBE2maptpB6i1MJ4uadY1AXlQOQ=
于 2013-09-17T12:17:57.143 回答