简体   繁体   English

如何使用Base64编码执行HMAC-SHA1?

[英]How to perform HMAC-SHA1 with Base64 Encode?

I'm trying to setup an app to sign with my URLs so they may authenticate but I can't seem to figure out how to replicate the code that I'm trying from the following page: https://help.sendowl.com/help/signed-urls#example 我正在尝试设置一个应用程序以使用我的URL签名,以便它们可以进行身份​​验证,但是我似乎无法从以下页面上找出如何复制我尝试的代码: https : //help.sendowl.com /帮助/签订的URL#示例

order_id=12345&buyer_name=Test+Man&buyer_email=test%40test.com&product_id=123&signature=QpIEZjEmEMZV%2FHYtinoOj5bqAFw%3D

buyer_email=test@test.com&buyer_name=Test Man&order_id=12345&product_id=123

buyer_email=test@test.com&buyer_name=Test Man&order_id=12345&product_id=123&secret=t0ps3cr3t

publicStr&t0ps3cr3t

This is the steps: 步骤如下:

  1. First order the parameters (removing the signature) and unescape them: 首先对参数排序(删除签名)并取消转义:
  2. Next append your Signing secret: 接下来,添加您的签名秘密:
  3. Generate the key to sign with: 生成用于签名的密钥:
  4. Perform the HMAC-SHA1 digest with Base 64 encode: QpIEZjEmEMZV/HYtinoOj5bqAFw= 使用Base 64编码执行HMAC-SHA1摘要: QpIEZjEmEMZV / HYtinoOj5bqAFw =

The following is what I tried but end up not getting the same result: 以下是我尝试过的结果,但最终没有得到相同的结果:

$signKey = "t0ps3cr3t";
$signData = "buyer_email=test@test.com&buyer_name=Test Man&order_id=12345&product_id=123&secret=t0ps3cr3t";

$passData = hash_hmac("sha1", $signData, base64_decode(strtr($signKey)), true);
$passData = base64_encode($passData);

echo $passData;

I keep getting x8NXmAmkNBPYCXwtj65mdVJ8lPc= 我不断收到x8NXmAmkNBPYCXwtj65mdVJ8lPc=

I was able to replicate with the following: took me a bit to figure out something so simple.. been coding for 11 hours straight. 我能够复制以下内容:花了一点时间弄清楚这么简单的东西..已经连续11个小时编码了。

Thanks. 谢谢。

$data = "buyer_email=test@test.com&buyer_name=Test Man&order_id=12345&product_id=123&secret=t0ps3cr3t";
$key = "publicStr&t0ps3cr3t";

$pass1 = hash_hmac('sha1', $data, $key, true);
$pass = base64_encode($pass1);

echo $pass;

$pass will return "QpIEZjEmEMZV/HYtinoOj5bqAFw=", the correct value.
$current_timestamp = Carbon::now()->timestamp;
    $signstring = "z001-line-anime-gif." . $current_timestamp . ".aaaabbbbccccdddd";
    $secret = 'STG-7f*(:hsM-1_eQ_ZD175QgEoJhI$:oR.zEQ<z';

    $sig = hash_hmac('sha1', $signstring, $secret);

    $signature = hex2bin($sig);

    $signature = base64_encode($signature);
    return $signature;

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

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