dplece1882 2018-07-10 14:44
浏览 142

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题