使用 HMAC384 为 Nominex 生成请求
Generate request for Nominex using HMAC384
我正在尝试向 Nominex 进行身份验证 API,我找不到此向 Nominex 请求的任何 C# 示例。文档只有 node.js 示例。我正在尝试将其转换为 C# 但运气不佳,不确定我做错了什么。我得到的结果是无效凭据
JavaScript
中的例子
const crypto = require('crypto')
const request = require('request')
const apiKey = '...'
const apiSecret = '...'
const apiPath = '/api/rest/v1/private/wallets'
const nonce = Date.now().toString()
const queryParams = ''
const body = undefined
let signature = `/api${apiPath}${nonce}${body ? JSON.stringify(body) : ''}`
const sig = crypto.createHmac('sha384', apiSecret).update(signature)
const shex = sig.digest('hex')
const options = {
url: `https://nominex.io/${apiPath}?${queryParams}`,
headers: {
'nominex-nonce': nonce,
'nominex-apikey': apiKey,
'nominex-signature': shex
},
body: body,
json: true
}
request.get(options, (error, response, body) => {
console.log(body);
})
我在 C# 中的尝试
private async Task<string> GetRequest()
{
HttpClient client = new HttpClient();
string secretKey = apiSecret;
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
long nonce = (long)t.TotalMilliseconds;
string body = "";
string requestPath = "/api/rest/v1/private/orders/BTC/USDT";
string prehashString = string.Join("", requestPath, nonce.ToString(), body);
string digest = "";
using (var hmacsha384 = new HMACSHA384(Encoding.Default.GetBytes(secretKey)))
{
byte[] hashmessage = hmacsha384.ComputeHash(Encoding.Default.GetBytes(prehashString));
bool upperCase = false;
StringBuilder result = new StringBuilder(hashmessage.Length * 2);
for (int i = 0; i < hashmessage.Length; i++)
result.Append(hashmessage[i].ToString(upperCase ? "X2" : "x2"));
digest = result.ToString();
}
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{requestPath}");
msg.Content = new StringContent(body, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("nominex-apikey", apiKey);
client.DefaultRequestHeaders.Add("nominex-nonce",nonce.ToString());
client.DefaultRequestHeaders.Add("nominex-signature", digest);
var response = await client.SendAsync(msg);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
签名是:
let signature = `/api${apiPath}${nonce}${body ? JSON.stringify(body) : ''}`
所以他们在 之前添加了一个 /api
apiPath
,在 nodejs 示例中是 /api/rest/v1/private/wallets
,所以在这种情况下签名会是(nonce
为 1609874861073
):
/api/api/rest/v1/private/wallets1609874861073
所以
string prehashString = string.Concat("/api", requestPath, nonce.ToString(), body);
并将所有Encoding.Default
替换为Encoding.UTF8
。他们不应该给你任何问题,但有问题就是有问题。
下次,如果你的电脑上安装了 nodejs,你可以在你的 nodejs 上尝试他们的代码,自由添加一些 console.log,否则有一个站点,repl.it,你可以尝试一些 node.js 代码(这就是我为解决您的问题所做的)。
我正在尝试向 Nominex 进行身份验证 API,我找不到此向 Nominex 请求的任何 C# 示例。文档只有 node.js 示例。我正在尝试将其转换为 C# 但运气不佳,不确定我做错了什么。我得到的结果是无效凭据
JavaScript
中的例子const crypto = require('crypto')
const request = require('request')
const apiKey = '...'
const apiSecret = '...'
const apiPath = '/api/rest/v1/private/wallets'
const nonce = Date.now().toString()
const queryParams = ''
const body = undefined
let signature = `/api${apiPath}${nonce}${body ? JSON.stringify(body) : ''}`
const sig = crypto.createHmac('sha384', apiSecret).update(signature)
const shex = sig.digest('hex')
const options = {
url: `https://nominex.io/${apiPath}?${queryParams}`,
headers: {
'nominex-nonce': nonce,
'nominex-apikey': apiKey,
'nominex-signature': shex
},
body: body,
json: true
}
request.get(options, (error, response, body) => {
console.log(body);
})
我在 C# 中的尝试
private async Task<string> GetRequest()
{
HttpClient client = new HttpClient();
string secretKey = apiSecret;
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
long nonce = (long)t.TotalMilliseconds;
string body = "";
string requestPath = "/api/rest/v1/private/orders/BTC/USDT";
string prehashString = string.Join("", requestPath, nonce.ToString(), body);
string digest = "";
using (var hmacsha384 = new HMACSHA384(Encoding.Default.GetBytes(secretKey)))
{
byte[] hashmessage = hmacsha384.ComputeHash(Encoding.Default.GetBytes(prehashString));
bool upperCase = false;
StringBuilder result = new StringBuilder(hashmessage.Length * 2);
for (int i = 0; i < hashmessage.Length; i++)
result.Append(hashmessage[i].ToString(upperCase ? "X2" : "x2"));
digest = result.ToString();
}
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{requestPath}");
msg.Content = new StringContent(body, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("nominex-apikey", apiKey);
client.DefaultRequestHeaders.Add("nominex-nonce",nonce.ToString());
client.DefaultRequestHeaders.Add("nominex-signature", digest);
var response = await client.SendAsync(msg);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
签名是:
let signature = `/api${apiPath}${nonce}${body ? JSON.stringify(body) : ''}`
所以他们在 之前添加了一个 /api
apiPath
,在 nodejs 示例中是 /api/rest/v1/private/wallets
,所以在这种情况下签名会是(nonce
为 1609874861073
):
/api/api/rest/v1/private/wallets1609874861073
所以
string prehashString = string.Concat("/api", requestPath, nonce.ToString(), body);
并将所有Encoding.Default
替换为Encoding.UTF8
。他们不应该给你任何问题,但有问题就是有问题。
下次,如果你的电脑上安装了 nodejs,你可以在你的 nodejs 上尝试他们的代码,自由添加一些 console.log,否则有一个站点,repl.it,你可以尝试一些 node.js 代码(这就是我为解决您的问题所做的)。