在php中生成数字签名

时间:2013-09-17 11:31:43

标签: php ruby digital-signature sha256

我过去常常在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=

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

输出:

2XBK7UXA9oDsfJj1TBE2maptpB6i1MJ4uadY1AXlQOQ=