【问题标题】:PHP - Create Signature using Nonce & Timestamp (JS & Python equivalent provided)PHP - 使用 Nonce 和时间戳创建签名(提供 JS 和 Python 等效项)
【发布时间】:2021-11-05 17:49:27
【问题描述】:

我正在尝试连接到 API,但它们仅提供 Python 和 Javascript 指南。 我正在尝试找到 php 的等价物,但结果不足。

更多参考:https://docs.deribit.com/#authentication

我无法在 PHP 中获得正确的签名。

Javascript:

var timestamp = Date.now();
var nonce = "abcd";
var data = "";
var signature = CryptoJS.HmacSHA256(`${timestamp}\n${nonce}\n${data}`, accessSecret).toString();

Python:

timestamp = round(datetime.now().timestamp() * 1000)
nonce = "abcd"
data = ""
signature = hmac.new(
    bytes(clientSecret, "latin-1"),
    msg=bytes('{}\n{}\n{}'.format(timestamp, nonce, data), "latin-1"),
    digestmod=hashlib.sha256
).hexdigest().lower()

PHP: (*我不知道如何创建随机数)

$timestamp = number_format(microtime(true) * 1000, 0, '.', '');
$nonce = number_format(microtime(true) * 1000, 0, '.', '');
$data = "";

$key = $timestamp.$nonce.$data;
$sign = hash_hmac('sha256',$key,$secret);
$sign = bin2hex($sign);

【问题讨论】:

  • 对于 PHP,$nonce = "abcd";
  • @KenLee - 这只是他们提供的一个示例。
  • 当然,您需要将abcd 更改为您想要的。 (我稍后会给你一个例子)
  • 什么确切不起作用?你有什么努力让它发挥作用?

标签: javascript python php api encryption


【解决方案1】:

对于随机生成需要'a-z0-9',8个字符 您可以使用chrrand 来完成这项工作。

因此尝试以下操作

<?php
$x1=   chr(rand(0,25)+97) ;
$x2=   chr(rand(0,9)+48) ;
$x3=   chr(rand(0,25)+97) ;
$x4=   chr(rand(0,9)+48) ;
$x5=   chr(rand(0,25)+97) ;
$x6=   chr(rand(0,9)+48) ;
$x7=   chr(rand(0,25)+97) ;
$x8=   chr(rand(0,9)+48) ;

$nonce = $x1.$x2.$x3.$x4.$x5.$x6.$x7.$x8;

?>

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2017-12-21
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多