我一直在尝试使用三个参数生成HMAC-rsa256签名; C#.Net中的Unix时间戳(秒),API密钥和API密钥,用于以特定样式进行身份验证,但无法对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)
}
但有了这个,我无法重新生成正确的样本签名。 Sample Apikey,Secret,timestamp及其产品签名:
apiSecret 1IuUeW4IEWatK87zBTENHj1T17s
时间戳 1448034533
apiKey 1WZbtMTbMbo2NsW12vOz9IuPM
签名 7d581adb01ad22f1ed38e1159a7f08ac5d83906ae1a42fe17e7d977786fe9694
相反,我最终得到的签名是:
我试图将参数逐个提供给加密函数,但效果不佳。
当我使用实际功能生成带有真实Api Key和Secret和实时时间戳的签名时,来自服务器的返回消息显示时间戳不正确(但我看不到任何问题),这也是馈入签名,这可能完全是由于时间戳不正确,但即使使用提供的样本ApiKey和Secret,测试函数也会产生完全不同的结构。 /“在其中更短”
到目前为止,使用Json请求函数没有问题,但是,以防万一,它应该是什么样子(给出的签名与上面的签名不同):
{ “e”:“auth”, “auth”:{ “key”:“1WZbtMTbMbo2NsW12vOz9IuPM。”, “签名”:“02483c01efc26fac843dd34d0342d269bacf4daa906a32cb71806eb7467dcf58”, “timestamp”:1448034533 } }
如果我错过了解释这个问题的任何现有主题,我会提前道歉,如果有人能够解释这个问题我会非常感激。
答案 0 :(得分:0)
您的方法返回签名的base64表示。
如果您改为返回hex1
,则会以大写形式获得预期结果。如果您想要小写字母,请返回hex1.ToLower();