这是我的一些JS代码。我正在使用Crypto创建hmac,然后传递日期和签名作为要在PHP中使用的请求标头:
events: {
proxyReq: (proxyReq, req) => {
const date = Date.now();
const API_KEY = 125;
const API_SECRET_KEY = 'abc';
const s = new Buffer(API_SECRET_KEY, 'base64');
const message = req.method + req.url + date;
let hmac = crypto.createHmac('sha512', s);
const signature = hmac.update(message).digest('base64');
proxyReq.setHeader('x-api-key', API_KEY);
proxyReq.setHeader('x-api-signature', signature);
proxyReq.setHeader('x-api-date', date);
},
},
这是我的PHP函数,在这里我尝试使用hash_hmac()复制相同的hmac:
public function handle($request, \Closure $next)
{
try {
$API_SECRET_KEY = 'abc';
$method = $request->method();
$url = $request->path();
$date = $request->header('x-api-date');
$client_key = $request->header('x-api-key');
$client_signature = $request->header('x-api-signature');
$message = $method . $url . $date;
$hmac = base64_encode(hash_hmac('sha512', $message, base64_decode($API_SECRET_KEY), true));
$all['x-api-date'] = $date;
$all['x-api-signature'] = $client_signature;
$all['x-api-key'] = $client_key;
Log::error($client_signature);
Log::error($hmac);
return $next($request);
}
我的最后输出:
[2018-07-10 14:23:57] local.ERROR: D0xis7BbxLBg6eWBlIT0ZSnhzk4c3Z3Ore9B16bNZow8xoinfM1zuMlS+lZ6pcOqRRHuupodUVFAIHfv89v4xw==
[2018-07-10 14:23:57] local.ERROR: Uypk5ZAd/I6E6sMi9UjIMyMxVhuQm7MatZbKCcGjGjsE2JqqrvTRYuJAR/SSde10eHAkPIz0g24CSZp0G+833A==
有人可以向我解释我做错了什么吗?我确保使用base64编码/解码,并确保传递给hmac函数的值与标头相同。
感谢您的帮助!