6

bitstamp 的新身份验证说明如下:

签名是 HMAC-SHA256 编码的消息,包含:nonce、客户端 ID 和 API 密钥。HMAC-SHA256 代码必须使用您的 API 密钥生成的密钥生成。此代码必须转换为它的十六进制表示(64 个大写字符)。示例(Python):message = nonce + client_id + api_key signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest()。上()

来源:链接

我有以下代码来添加新签名(和其他参数):

public void AddApiAuthentication(RestRequest restRequest)
    {
        var nonce = DateTime.Now.Ticks;
        var signature = GetSignature(nonce, apiKey, apiSecret, clientId);

        restRequest.AddParameter("key", apiKey);
        restRequest.AddParameter("signature", signature);
        restRequest.AddParameter("nonce", nonce);

    }

    private string GetSignature(long nonce, string key, string secret, string clientId)
    {
        string msg = string.Format("{0}{1}{2}", nonce,
            clientId,
            key);

        return ByteArrayToString(SignHMACSHA256(secret, StrinToByteArray(msg))).ToUpper();
    }
    public static byte[] SignHMACSHA256(String key, byte[] data)
    {
        HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
        return hashMaker.ComputeHash(data);
    }

    public static byte[] StrinToByteArray(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

    public static string ByteArrayToString(byte[] hash)
    {
        return BitConverter.ToString(hash).Replace("-", "").ToLower();
    }

然后我得到这个错误:

{“错误”:“无效签名”}

任何人都知道问题可能是什么?我检查了我的参数 100 次,这些都没有错。也许有人为新的身份验证获得了一段工作代码(在 C# 中)?

更新

Abhinav 是对的,StringToByteArray 方法是错误的(不仅是错字:P),工作代码是:

public static byte[] StrinToByteArray(string str)
    {
        return System.Text.Encoding.ASCII.GetBytes(str);
    }
4

1 回答 1

5

您使用str.ToCharArray()StrinToByteArray是不正确的(仅在同一系统上使用时正确)。你需要使用 ASCII 编码什么的。

于 2013-10-31T21:52:22.307 回答