繁体   English   中英

将Python代码转换为PHP/Laravel(Gate.io签名)

[英]Convert Python code to PHP/Laravel (Gate.io signature)

我正在合并门,io rest api,目前正在尝试将签名 function 从 python 转换为 php(laravel)。

显然转换中隐藏了一个错误。

有人可以看一下并告诉我这是否全部正确或者这里是否缺少某些内容?

对于改进建议,我将不胜感激

Python 代码:

def gen_sign(method, url, query_string=None, payload_string=None):
    key = ''        # api_key
    secret = ''     # api_secret

    t = time.time()
    m = hashlib.sha512()
    m.update((payload_string or "").encode('utf-8'))
    hashed_payload = m.hexdigest()
    s = '%s\n%s\n%s\n%s\n%s' % (method, url, query_string or "", hashed_payload, t)
    sign = hmac.new(secret.encode('utf-8'), s.encode('utf-8'), hashlib.sha512).hexdigest()
    return {'KEY': key, 'Timestamp': str(t), 'SIGN': sign}

来源:Gate.io API 签名串生成

Php 代码:

public function createSignature($method, $url, $query=null, $payload=null, $algo = 'sha256'){
        $key = 'xxx';
        $secret= 'xxx';

        $time = microtime(true);

        $hashed_payload = hash_hmac($algo,$query ?? '');

        $string = "{$methode}\n{$url}\n{$query ?? ''}\n{$hashed_payload}\n{$time}"
        
        $sign = hash_hmac($algo,$string,$secret)
       
        return ['KEY' => $key, 'Timestamp' => "{$time}", 'SIGN' => $sign]
    
}

我得到了答案,我希望它会有所帮助:

public function buildSignHeaders($method, $resourcePath, $queryParams = [], $payload = null)
    {
        $fullPath = parse_url(config('gate-io.host'), PHP_URL_PATH) . $resourcePath;
        $fmt = "%s\n%s\n%s\n%s\n%s";
        $timestamp = time();
        $hashedPayload = hash("sha512", ($payload !== null) ? $payload : "");
        $signatureString = sprintf(
            $fmt,
            $method,
            $fullPath,
            GuzzleHttp\Psr7\build_query($queryParams, false),
            $hashedPayload,
            $timestamp
        );
        $signature = hash_hmac("sha512", $signatureString, config('gate-io.apiSecretKey'));
        return [
            "KEY" => config('gate-io.apiKey'),
            "SIGN" => $signature,
            "Timestamp" => $timestamp
        ];
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM