【问题标题】:Generating HMAC-rsa256 Signature in C# from a Python sample从 Python 示例在 C# 中生成 HMAC-rsa256 签名
【发布时间】:2017-05-20 15:04:37
【问题描述】:

我一直在尝试使用三个参数生成 HMAC-rsa256 签名;以秒为单位的 Unix 时间戳、API 密钥和 API 机密,在 C#.Net 中用于特定样式的身份验证,但无法从 Python 中的示例代码中逆向工程步骤。

我试图从中分解步骤的 Python 代码是:

import hmac
import hashlib
import datetime
import json

def create_signature_Py27(key, secret):  # (string key, string secret) 
    timestamp = int(datetime.datetime.now().timestamp())  # UNIX timestamp in seconds
    string = "{}{}".format(timestamp, key)
    return hmac.new(secret, string, hashlib.sha256).hexdigest()

# Python 2.7 - 3.5+ 
# Note: latest versions of hmac lib requires 'secret' and 'string' as byte strings but not unicode
#
def create_signature(key, secret):  # (string key, string secret) 
    timestamp = int(datetime.datetime.now().timestamp())  # UNIX timestamp in seconds
    string = "{}{}".format(timestamp, key)
    return timestamp, hmac.new(secret.encode(), string.encode(), hashlib.sha256).hexdigest()

def auth_request(key, secret):
    timestamp, signature = create_signature(key, secret)
    return json.dumps({'e': 'auth',
        'auth': {'key': key, 'signature': signature, 'timestamp': timestamp,}, 'oid': 'auth', })

auth_request = auth_request('1WZbtMTbMbo2NsW12vOz9IuPM', '1IuUeW4IEWatK87zBTENHj1T17s'):

到目前为止我所做的尝试:

       public static string TEST(string timestamp4, string apikey1, string apisecret1)
       {


           HMACSHA256 hmc1 = new HMACSHA256(Encoding.ASCII.GetBytes(apisecret1));
           string mes = timestamp4 + apikey1;
           MessageBox.Show(mes);

           byte[] hmres1 = hmc1.ComputeHash(Encoding.ASCII.GetBytes(mes));
           string hex1 = BitConverter.ToString(hmres1).Replace("-", string.Empty);
           string b641 = Convert.ToBase64String(hmres1).Replace("=", "");
           MessageBox.Show(b641, "b64");
           return b641;                   // THIS TEST SIGNITURE RETURNS SOMETHING LIKE "asdaefcdvcxv/sdfdsfsd/fsd" , WHERE DOES THE "/" COMES FROM?
                                         //TIMESTAMP RETURNS WRONG VALUE WHICH THEN FEED INTO SIGNITURE FUNCTION- GIVING INVALID SIGNITURE( but its unix timestamp in sec's whish is expected)
}

但是有了这个,我无法重新生成正确的样本签名。示例 Apikey、Secret、时间戳及其产品签名:

  • apiSecret 1IuUeW4IEWatK87zBTENHj1T17s

  • 时间戳 1448034533

  • apiKey 1WZbtMTbMbo2NsW12vOz9IuPM

  • 签名 7d581adb01ad22f1ed38e1159a7f08ac5d83906ae1a42fe17e7d977786fe9694

相反,我最终得到的签名是:

  • 生成的签名 3EHLmGeh0jK9vRLgh/0j5LgQU6h/nijzj4G9KBBjv10

我曾尝试将参数一一提供给加密函数,但效果不佳。

当我使用 real 函数 生成具有真实 Api Key 和 Secret 以及实时时间戳的签名时,服务器返回的消息显示时间戳不正确(但我看不到任何问题),这也被输入到签名中,这可能完全是由于时间戳不正确,但即使使用提供的示例 ApiKey 和 Secret,测试函数也会生成完全不同的结构。(使用“ /" 在里面并且更短)

到目前为止,Json请求功能没有问题,但是,以防万一,它应该是这样的(给她的签名与上面不同):

{ “e”:“认证”, “认证”:{ "key": "1WZbtMTbMbo2NsW12vOz9IuPM.", “签名”:“02483c01efc26fac843dd34d0342d269bacf4daa906a32cb71806eb7467dcf58”, “时间戳”:1448034533 } }

如果我错过了解释此问题的任何现有主题,我提前道歉,如果有人能阐明这个问题,我将不胜感激。

【问题讨论】:

    标签: c# python signature hmac


    【解决方案1】:

    您的方法返回签名的 base64 表示。

    如果您改为返回hex1,则会得到大写的预期结果。如果你想要小写字母,返回hex1.ToLower();

    【讨论】:

    • 谢谢,降低了工作,但时间戳仍然返回错误,因此签名无效。
    • 时间戳见stackoverflow.com/questions/17632584/…,你没有发布你如何获得时间戳的逻辑,所以我不能告诉你有什么问题。
    • 找到了问题。json类中的错字...原来json有问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多