我正在尝试使它工作平稳,但无法获得相同的结果。我的php代码先打印不同的哈希,然后再打印flutter代码。在flutter应用程序中可以这样做吗?

我已经尝试通过运行此Flutter代码来实现这一目标。但是经过5个小时的阅读,我放弃了,创建了一个堆栈溢出帐户。import 'package:crypto/crypto.dart';

import 'dart:convert'; // for the utf8.encode method

import 'package:http/http.dart' as http;

void main() {

var api = 'https://app.repricer.nl';

var endpoint = '/api/v1/channels/all.json';

var method = 'GET';

var public_key = '';

var private_key = '';

var data = '';

var ms = (new DateTime.now()).millisecondsSinceEpoch;

var timestamp = ms / 1000;

var hash_string = public_key + '|' + method + '|' + endpoint + '|' + data + '|' + timestamp.toString();

var key = utf8.encode(private_key);

var bytes = utf8.encode(hash_string);

var hmacSha256 = new Hmac(sha512, key); // HMAC-SHA256

var digest = hmacSha256.convert(bytes);

print(digest);

}

这是我想转换为颤动的PHP代码: $api = 'https://app.repricer.nl';

$endpoint = '/api/v1/channels/all.json';

$method = 'GET';

$public_key = '';

$private_key = '';

// Generate the CURL headers to authenticate our request

$headers = generateHash($public_key, $private_key, $method, $endpoint, $data);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$api.$endpoint);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

curl_close($ch);

print_r($result);

function generateHash($public_key, $private_key, $method, $endpoint, $data)

{

$timestamp = date("U");

$hash_string = array($public_key,$method,$endpoint,$data,$timestamp);

$hash = hash_hmac('sha512',implode('|',$hash_string),$private_key);

print ($hash);

return array('X-Auth: '.$public_key, 'X-Hash: '.$hash, 'X-Date: '.$timestamp);

}

我希望输出与时间戳相同。但是我在同一秒内运行了它,这是2个完全不同的结果。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐