[英]Equivalent python hmac method in c#
I'm trying to pair my Philips TV in a .NET program i am wrting.我正在尝试将我的飞利浦电视与我正在编写的 .NET 程序配对。
I found this awessome Python library nammed pylips and I am using it as a reference, but having a hard time with the signature method.我发现这个很棒的 Python 库命名为pylips ,我将它用作参考,但很难使用签名方法。
I dont know python, and have tried mutiple solution to reproduce same signature in c#我不知道 python,并尝试了多种解决方案来重现 c# 中的相同签名
auth["auth_signature"] = self.create_signature(b64decode(secret_key), str(auth_Timestamp).encode() + str(pin).encode())
def create_signature(self, secret_key, to_sign):
sign = HMAC.new(secret_key, to_sign, SHA)
return str(b64encode(sign.hexdigest().encode()))
If i run "create_signature" in python with following arguments如果我在 python 中运行“create_signature”,并遵循 arguments
to_sign= 1400493467
secret_key = JCqdN5AcnAHgJYseUn7ER5k3qgtemfUvMRghQpTfTZq7Cvv8EPQPqfz6dDxPQPSu4gKFPWkJGw32zyASgJkHwCjU
I get this output我得到这个 output
b'Y2NiNDQ4ZDcyYjg5ZmJlNzg1MWNiYjIyYzEwOGNhMTJmNWZmODYwNA=='
Normally, I have at least a couple of questions, but I think I can show a simple answer.通常,我至少有几个问题,但我想我可以给出一个简单的答案。
Your to_sign
is a concatenated string of a timestamp auth_Timestamp
and some pin
.您的to_sign
是时间戳auth_Timestamp
和一些pin
的串联字符串。
You have to see how this timestamp is generated (in my example I imply it is the Unix-Time).您必须查看此时间戳是如何生成的(在我的示例中,我暗示它是 Unix 时间)。
The pin - is this the message
actually or some other value (in my example I imply it is the message)? pin - 这实际上是message
还是其他值(在我的示例中,我暗示它是消息)?
Which SHA algorithm are you using ( SHA
)?您使用的是哪种 SHA 算法( SHA
)? Because the correct HMAC implementation depends on this (in my example I imply SHA256).因为正确的 HMAC 实现取决于此(在我的示例中,我暗示 SHA256)。
static void Main(string[] args) { int message = 1400493467; string secret_key = "key"; string auth_Timestamp = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();// is this the Unix-Time string pin = message.ToString(); // is this the message? string to_sign = auth_Timestamp + pin;//str(auth_Timestamp).encode() + str(pin).encode() string result = create_signature(secret_key, to_sign); Console.WriteLine(result); } private static string create_signature(string secret_key, string to_sign) { // Which SHA are you using? SHA256 (HMACSHA256), SHA1 (HMACSHA1) or something else using (var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(secret_key))) { var hash = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(to_sign)); return Convert.ToBase64String(hash); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.