PHP sha512 hmac产生的值与JS hmac不同

Here is some of my JS code. I am creating the hmac using Crypto, then passing in the date and signature as request headers to use in 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);
    },
},

Here is my PHP function where I try to reproduce the same hmac using hash_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);
}

My last outputs:

[2018-07-10 14:23:57] local.ERROR: D0xis7BbxLBg6eWBlIT0ZSnhzk4c3Z3Ore9B16bNZow8xoinfM1zuMlS+lZ6pcOqRRHuupodUVFAIHfv89v4xw==  
[2018-07-10 14:23:57] local.ERROR: Uypk5ZAd/I6E6sMi9UjIMyMxVhuQm7MatZbKCcGjGjsE2JqqrvTRYuJAR/SSde10eHAkPIz0g24CSZp0G+833A== 

Can someone explain to me what I am doing wrong? I made sure to use base64 encoding/decoding and made sure the values passed into the hmac function were the same as the headers.

I appreciate any help!

dongyushen9063
dongyushen9063 我使用'abc'作为密钥。我将api_secret设置为'abc'。对此我很抱歉,我应该保持变量的一致性和显着性。修复它以获得更好的可读性。
大约 2 年之前 回复
dongmou9260
dongmou9260 看起来您在客户端上使用125键进行编码,但在服务器上使用abc。
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问