简体   繁体   English

在php生成数字签名

[英]Generating digital signature in php

I used to generate digital signature in Ruby like so:我曾经像这样在 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=

How to achieve the same with Php, I'm using the following code in php, but the outputs are different如何与 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=

Any ideas?有任何想法吗?

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=

I have a solution for your probleme我有解决你问题的方法

 $("#btnSaveSign").click(function(e){ html2canvas([document.getElementById('sign-pad')], { onrendered: function (canvas) { var canvas_img_data = canvas.toDataURL('image/png'); var img_data = canvas_img_data.replace(/^data:image\/(png|jpg);base64,/, ""); //ajax call to save image inside folder $.ajax({ url: 'save_sign.php', data: { img_data:img_data }, type: 'post', dataType: 'json', success: function (response) { window.location.reload(); } }); } }); });

jquery digital signature jquery 数字签名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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