简体   繁体   English

从Python示例生成C#中的HMAC-rsa256签名

[英]Generating HMAC-rsa256 Signature in C# from a Python sample

I've been trying to generate HMAC-rsa256 signature using three parameters; 我一直在尝试使用三个参数生成HMAC-rsa256签名; a Unix timestamp in seconds, an API Key and an API Secret, in C#.Net for authentication in a spesific style, but couldn't manage to reverse-engineer the steps from an example code in Python. C#.Net中以秒为单位的Unix时间戳,API密钥和API密钥,以一种特殊的样式进行身份验证,但是无法通过Python中的示例代码对这些步骤进行反向工程。

The Python code I'm trying to dechiper the steps from is: 我正在尝试对以下步骤进行去屑处理的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'):

What I've tried so far: 到目前为止,我已经尝试过:

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

But with this, I cannot re-generate the correct sample signature. 但是,由此,我无法重新生成正确的样本签名。 The Sample Apikey,Secret,timestamp and their product signature: Sample Apikey,Secret,timestamp及其产品签名:

  • apiSecret 1IuUeW4IEWatK87zBTENHj1T17s apiSecret 1IuUeW4IEWatK87zBTENHj1T17s

  • timestamp 1448034533 时间戳 1448034533

  • apiKey 1WZbtMTbMbo2NsW12vOz9IuPM apiKey 1WZbtMTbMbo2NsW12vOz9IuPM

  • signature 7d581adb01ad22f1ed38e1159a7f08ac5d83906ae1a42fe17e7d977786fe9694 签名 7d581adb01ad22f1ed38e1159a7f08ac5d83906ae1a42fe17e7d977786fe9694

Instead, the signature I end up with is: 相反,我最终得到的签名是:

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

I have tried to feed the parameters one by one to the encryption function but that didn't go well. 我试图将参数一个接一个地提供给加密功能,但是效果并不理想。

When I use the real function to generate a signature with the real Api Key and Secret and realtime timestamp, the returned message from the server shows that timestamp is incorrect(but I couldn't see any problem), and that is also feed into the signature, which could have been solely due to the incorrect timestamp but even with the provided sample ApiKey and Secret, the test function generates entirely different structure.(with "/" in it and shorter) 当我使用real函数使用真实的Api Key和Secret以及实时时间戳生成签名时,服务器返回的消息表明时间戳不正确(但我看不到任何问题),并且这也馈入了签名,可能完全是由于时间戳不正确造成的,但是即使使用提供的示例ApiKey和Secret, 测试函数也会生成完全不同的结构。(其中带有“ /”且更短)

There is no problem so far with the Json request function, but, just in case, this is what it should look like(the signature given her differs from the above): 到目前为止,Json请求功能没有问题,但是,以防万一,这应该是什么样子(给她的签名与上面的不同):

{ "e": "auth", "auth": { "key": "1WZbtMTbMbo2NsW12vOz9IuPM.", "signature": "02483c01efc26fac843dd34d0342d269bacf4daa906a32cb71806eb7467dcf58", "timestamp": 1448034533 } } {“ e”:“ auth”,“ auth”:{“ key”:“ 1WZbtMTbMbo2NsW12vOz9IuPM。”,“ signature”:“ 02483c01efc26fac843dd34d0342d269bacf4daa906a32cb71806eb7467dcf58”,“ timestamp”:1448034533}

I apologize in advance if I missed any existing topics explaining this problem, and I'd greatly appreciate if someone could shed some light on this issue. 如果我错过任何现有的话题来解释这个问题,我谨此致歉,如果有人能对此问题有所启发,我将不胜感激。

Your method returns the base64 representation of the signature. 您的方法返回签名的base64表示形式。

If you return hex1 instead, you get the expected result in upper case. 如果返回hex1hex1获得预期结果。 If you want lower case letters, return hex1.ToLower(); 如果要使用小写字母,请返回hex1.ToLower();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM