在C#中无法获得与python中相同的哈希 [英] Cannot get same hash in C# as in python
问题描述
我有一个字符串,需要对其进行哈希处理才能访问API. API创建者已在Python中提供了一个代码段,该代码段对如下代码进行了散列:
hashed_string = hashlib.sha1(string_to_hash).hexdigest()
使用此哈希字符串访问API时,一切都很好.我试图在C#中获得相同的哈希字符串结果,但没有成功.我已经尝试了许多难以置信的方法,但到目前为止没有任何效果.我也知道最十六进制的部分,因此在尝试模仿行为时请牢记这一点.
有人知道如何在C#中获得相同的结果吗?
这是我尝试在C#中重现相同结果的多种方法之一:
public string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString().ToLower();
}
}
此代码摘自:在C#中使用SHA1算法进行哈希处理
另一种方式
public string ToHexString(string myString)
{
HMACSHA1 hmSha1 = new HMACSHA1();
Byte[] hashMe = new ASCIIEncoding().GetBytes(myString);
Byte[] hmBytes = hmSha1.ComputeHash(hashMe);
StringBuilder hex = new StringBuilder(hmBytes.Length * 2);
foreach (byte b in hmBytes)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
此代码摘自: Python hmac和C#hmac >
编辑2
某些输入/输出:
C#(使用上述说明中提供的第二种方法)
输入:呼叫者ID1495610997apiKey3 * _& E#N @ B1)O)-1Y
输出:1ecded2b66e152f0965adb96727d96b8f5db588a
Python
输入:呼叫者ID1495610997apiKey3 * _& E#N @ B1)O)-1Y
输出:bf11a12bbac84737a39152048e299fa54710d24e
C#(使用上述说明中提供的第一种方法)
输入:callerId1495611935 apiKey {[B {+%P)s; WD5& 5x
输出:7e81e0d40ff83faf1173394930443654a2b39cb3
Python
输入:callerId1495611935 apiKey {[B {+%P)s; WD5& 5x
输出:512158bbdbc78b1f25f67e963fefdc8b6cbcd741
C#:
public static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("x2")); // x2 is lowercase
}
return sb.ToString().ToLower();
}
}
public static void Main()
{
var x ="callerId1495611935apiKey{[B{+%P)s;WD5&5x";
Console.WriteLine(Hash(x)); // prints 7e81e0d40ff83faf1173394930443654a2b39cb3
}
Python
import hashlib
s = u'callerId1495611935apiKey{[B{+%P)s;WD5&5x'
enc = s.encode('utf-8') # encode in utf8
hash = hashlib.sha1(enc)
formatted = h.hexdigest()
print(formatted) # prints 7e81e0d40ff83faf1173394930443654a2b39cb3
您的主要问题是,对于C#和Python中的同一字符串,您正在使用不同的编码.两种语言都使用UTF8,并使用相同的大小写.输出是相同的.
请注意,在您的输入字符串(在 This code is taken from: Hashing with SHA1 Algorithm in C# Another way This code is taken from: Python hmac and C# hmac EDIT 2 Some input/output: C# (using second method provided in above description) input: callerId1495610997apiKey3*_&E#N@B1)O)-1Y output: 1ecded2b66e152f0965adb96727d96b8f5db588a Python input: callerId1495610997apiKey3*_&E#N@B1)O)-1Y output: bf11a12bbac84737a39152048e299fa54710d24e C# (using first method provided in above description) input: callerId1495611935apiKey{[B{+%P)s;WD5&5x output: 7e81e0d40ff83faf1173394930443654a2b39cb3 Python input: callerId1495611935apiKey{[B{+%P)s;WD5&5x output: 512158bbdbc78b1f25f67e963fefdc8b6cbcd741callerId1495611935
和apiKey{[B{+%P)s;WD5&5x
之间)中,有一个隐藏 public string ToHexString(string myString)
{
HMACSHA1 hmSha1 = new HMACSHA1();
Byte[] hashMe = new ASCIIEncoding().GetBytes(myString);
Byte[] hmBytes = hmSha1.ComputeHash(hashMe);
StringBuilder hex = new StringBuilder(hmBytes.Length * 2);
foreach (byte b in hmBytes)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
C#:
public static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("x2")); // x2 is lowercase
}
return sb.ToString().ToLower();
}
}
public static void Main()
{
var x ="callerId1495611935apiKey{[B{+%P)s;WD5&5x";
Console.WriteLine(Hash(x)); // prints 7e81e0d40ff83faf1173394930443654a2b39cb3
}
Python
import hashlib
s = u'callerId1495611935apiKey{[B{+%P)s;WD5&5x'
enc = s.encode('utf-8') # encode in utf8
hash = hashlib.sha1(enc)
formatted = h.hexdigest()
print(formatted) # prints 7e81e0d40ff83faf1173394930443654a2b39cb3
Your main problem is that you are using different encodings for the same string in C# and Python. Use UTF8 in both languages and use the same casing. The output is the same.
Note that inside your input string (between callerId1495611935
and apiKey{[B{+%P)s;WD5&5x
) there is an hidden \u200b
character. That's why encoding your string in UTF-8 gives a different result than encoding it using ASCII. Does that character have to be inside your string?
这篇关于在C#中无法获得与python中相同的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!