【问题标题】:Bitstamp - new authentication in C# - signatureBitstamp - C# 中的新身份验证 - 签名
【发布时间】:2013-11-12 01:59:52
【问题描述】:

bitstamp 的新认证说明如下:

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

来源:link

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

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();
    }

然后我得到这个错误:

{"error": "无效签名"}

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

更新

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

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

【问题讨论】:

标签: c# web-services security hash bitcoin


【解决方案1】:

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

【讨论】:

  • 你是我的英雄!微小的变化(见更新)使代码工作。也可以使用 UTF8 而不是我在测试后注意到的 ASCII。
猜你喜欢
  • 2014-03-03
  • 1970-01-01
  • 2022-06-28
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2015-12-03
相关资源
最近更新 更多