Различные HmacSHA256 в PHP и Javascript - PullRequest
0 голосов
/ 07 мая 2020

это javascript код, предоставленный в API и работающий в POSTMAN

let time = new Date().getTime();

let body = {"serviceType":"MOTORCYCLE","specialRequests":[],"requesterContact":{"name":"Draco Yam","phone":"+6652344758"},"stops":[{"location":{"lat":"13.7099019","lng":"100.5724553"},"addresses":{"en_TH":{"displayString":"Terminal 15","country":"TH"}}},{"location":{"lat":"13.7238796","lng":"100.5542005"},"addresses":{"en_TH":{"displayString":"Terminal 21, 88 Soi Sukhumvit 19, Khlong Toei Nuea, Thawi Watthana, Bangkok 10110, Thailand","country":"TH"}}}],"deliveries":[{"toStop":1,"toContact":{"name":"Brian Garcia","phone":"+6652344837"},"remarks":"ORDER #: 1234, ITEM 1 x 1, ITEM 2 x 2"}]}

body = JSON.stringify(body);

let signature = CryptoJS.HmacSHA256(`${time.toString()}\r\nPOST\r\n/v2/quotations\r\n\r\n${body}`, environment['secret']);

postman.setEnvironmentVariable('signature', signature.toString());
postman.setEnvironmentVariable('time', time);
postman.setEnvironmentVariable('body', body);

это мой PHP код для генерации подписи

function buildHeader($args) {
    $time = time() * 1000;
    
    $key = $this->key;
    $secret = $this->secret;
    $signature = getSignature([
            'time' => $time,
            'method' => $args['method'],
            'path' => $args['path'],
            'body' => $args['body'],
            'secret' => $secret,
        ]);
    $header = [
        "Content-type" => "application/json; charset=utf-8",
        "Authorization" => "hmac " . $key . ":" . $time . ":" . $signature,
        "Accept" => "application/json",
        "X-LLM-Country" => $args['country'],
    ];
    return $header;
}
function getSignature($args) {

        $_encryptBody = '';
        if ($args['method'] == "GET") {
            $_encryptBody = $args['time'] . "\r\n" . $args['method'] . "\r\n" . $args['path'] . "\r\n\r\n";
        } else {
            $_encryptBody = $args['time'] . "\r\n" . $args['method'] . "\r\n" . $args['path'] . "\r\n\r\n" . json_encode($args['body']);
        }
        
        $sign = hash_hmac("sha256", $_encryptBody, $args['secret'], false);

        return $sign;
    }

$args = ['body' => $body, 'path' => $path, 'method' => 'POST', 'country'=>'TH'];       

    $header = buildHeader($args);  

И php, и javascript имеют разные подписи. Я застрял на нем последние 3 дня. Подскажите, пожалуйста, в чем ошибка кода PHP.

Заранее спасибо

...