Kucoin C#签名发行

Kucoin C#签名发行,c#,C#,我正在尝试将bot写入Kucoin API,但由于创建签名而失败 var input = nonce; var stringToSign = "/user/info/"+input+"/"; byte[] secretkeyBytes = Encoding.UTF8.GetBytes(SecretKey); byte[] inputBytes = Encoding.UTF8.GetBytes(stringToSign); using (var hmac = new HMACSHA256(secr

我正在尝试将bot写入Kucoin API,但由于创建签名而失败

var input = nonce;
var stringToSign = "/user/info/"+input+"/";
byte[] secretkeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(stringToSign);
using (var hmac = new HMACSHA256(secretkeyBytes))
{
byte[] hashValue = hmac.ComputeHash(inputBytes);
return BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}
但任何时候都会出现错误:

{“code”:“UNAUTH”,“msg”:“签名验证” 失败,“成功”:false,“时间戳”:1}

在官方网站上,我们有这样的例子

String strForSign = endpoint + "/" + nonce +"/" + queryString;  

//Make a base64 encoding of the completed string
String signatureStr = Base64.getEncoder().encodeToString(strForSign.getBytes("UTF-8"));

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secretKeySpec);

//KC-API-SIGNATURE in header
String signatureResult =  Hex.encodeHexString(sha256_HMAC.doFinal(signatureStr.getBytes("UTF-8")));

我的代码出了什么问题?

您错过了输入数据的base64编码步骤。你需要像这样的东西:

// String interpolation just to be simpler; no need for input variable either
var stringToSign = $"/user/info/{nonce}/";

// This is the step you were missing
var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));

byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
using (var hmac = new HMACSHA256(secretKeyBytes))
{
    byte[] hashValue = hmac.ComputeHash(inputBytes);
    return BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}

您错过了输入数据的base64编码步骤。你需要像这样的东西:

// String interpolation just to be simpler; no need for input variable either
var stringToSign = $"/user/info/{nonce}/";

// This is the step you were missing
var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));

byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
using (var hmac = new HMACSHA256(secretKeyBytes))
{
    byte[] hashValue = hmac.ComputeHash(inputBytes);
    return BitConverter.ToString(hashValue).Replace("-", "").ToLower();
}

我想发布我的代码,它可以工作并且包含正确的参数,因为Zimsan的代码包含参数的小错误

    public KuCoin_Balance GetAccountBalance()
    {
        KuCoin_Balance tmpBalance = new KuCoin_Balance();
        string domain = "https://api.kucoin.com";
        string endpoint = "/v1/user/info";
        string signatureResult = "";
        string SecretKey = apiSecret;
        long nonce = GetNonce();

        var stringToSign = $"/v1/user/info/{nonce}/";

        var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));

        byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
        byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
        using (var hmac = new HMACSHA256(secretKeyBytes))
        {
            byte[] hashValue = hmac.ComputeHash(inputBytes);
            signatureResult = BitConverter.ToString(hashValue).Replace("-", "").ToLower();
        }

        KuCoin_Response Resp = new KuCoin_Response();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain + endpoint);
        request.Method = "GET";
        request.Headers["KC-API-KEY"] = apiKey;
        request.Headers["KC-API-NONCE"] = nonce.ToString();
        request.Headers["KC-API-SIGNATURE"] = signatureResult; // apiSecret;
        HttpWebResponse response = null;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        Resp.StatusCode = "1";
                        Resp.StatusDescription = "Success";
                        Resp.ResponseString = reader.ReadToEnd();
                        //return Resp;
                    }//End of StreamReader
                }
            }// End of using ResponseStream
        }
        catch (Exception ex)
        {
            Resp.StatusCode = "-1";
            Resp.StatusDescription = "Error";
            Resp.ResponseString = ex.Message.ToString() + "      " + response.StatusDescription;
            //return Resp;
        }
        return tmpBalance;
    }

我想发布我的代码,它可以工作并且包含正确的参数,因为Zimsan的代码包含参数的小错误

    public KuCoin_Balance GetAccountBalance()
    {
        KuCoin_Balance tmpBalance = new KuCoin_Balance();
        string domain = "https://api.kucoin.com";
        string endpoint = "/v1/user/info";
        string signatureResult = "";
        string SecretKey = apiSecret;
        long nonce = GetNonce();

        var stringToSign = $"/v1/user/info/{nonce}/";

        var signatureString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));

        byte[] secretKeyBytes = Encoding.UTF8.GetBytes(SecretKey);
        byte[] inputBytes = Encoding.UTF8.GetBytes(signatureString);
        using (var hmac = new HMACSHA256(secretKeyBytes))
        {
            byte[] hashValue = hmac.ComputeHash(inputBytes);
            signatureResult = BitConverter.ToString(hashValue).Replace("-", "").ToLower();
        }

        KuCoin_Response Resp = new KuCoin_Response();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain + endpoint);
        request.Method = "GET";
        request.Headers["KC-API-KEY"] = apiKey;
        request.Headers["KC-API-NONCE"] = nonce.ToString();
        request.Headers["KC-API-SIGNATURE"] = signatureResult; // apiSecret;
        HttpWebResponse response = null;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        Resp.StatusCode = "1";
                        Resp.StatusDescription = "Success";
                        Resp.ResponseString = reader.ReadToEnd();
                        //return Resp;
                    }//End of StreamReader
                }
            }// End of using ResponseStream
        }
        catch (Exception ex)
        {
            Resp.StatusCode = "-1";
            Resp.StatusDescription = "Error";
            Resp.ResponseString = ex.Message.ToString() + "      " + response.StatusDescription;
            //return Resp;
        }
        return tmpBalance;
    }

尝试此操作,但出现相同的错误var stringToSign=“/user/info/”+input+”/”;var signatureString=Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));byte[]secretKeyBytes=Encoding.UTF8.GetBytes(secretKey);byte[]inputBytes=Encoding.UTF8.GetBytes(signatureString);使用(var hmac=new HMACSHA256(secretKeyBytes)){byte[]hashValue=hmac.ComputeHash(inputBytes);返回BitConverter.ToString(hashValue)。Replace(“-”,”).ToLower();}@Zimsan:在注释中放入大量代码实际上并不能最终实现可读性,但如果它与我提供的代码完全相同,没有明显的理由说明这是错误的。你能用同样的输入尝试Java代码吗?是的,它是一样的。我可以尝试,但对于这个项目,我必须只使用c#或js(不是节点js),它正在工作!!!谢谢,在参数中找到问题,它正在与您的代码一起工作)@Zimsan:Phew-我很高兴,否则我不知道下一步该去哪里:)尝试此操作,但出现相同的错误var stringToSign=“/user/info/”+input+”/”;var signatureString=Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToSign));byte[]secretKeyBytes=Encoding.UTF8.GetBytes(secretKey);byte[]inputBytes=Encoding.UTF8.GetBytes(signatureString);使用(var hmac=new HMACSHA256(secretKeyBytes)){byte[]hashValue=hmac.ComputeHash(inputBytes);返回BitConverter.ToString(hashValue)。Replace(“-”,”).ToLower();}@Zimsan:在注释中放入大量代码实际上并不能最终实现可读性,但如果它与我提供的代码完全相同,没有明显的理由说明这是错误的。你能用同样的输入尝试Java代码吗?是的,它是一样的。我可以尝试,但对于这个项目,我必须只使用c#或js(不是节点js),它正在工作!!!谢谢,在参数中找到问题,它正在使用您的代码)@Zimsan:Phew-我很高兴,否则我就不知道下一步该去哪里:)