无法在 C# 中获得与 python 中相同的哈希值
Cannot get same hash in C# as in python
我有一个字符串需要散列才能访问 API。 API-creator 在 Python 中提供了一个代码片段,它对代码进行了哈希处理,如下所示:
hashed_string = hashlib.sha1(string_to_hash).hexdigest()
当使用这个散列字符串访问 API 时,一切正常。我试图在 C# 中获得相同的散列字符串结果,但没有成功。我已经尝试了很多方法,但到目前为止没有任何效果。我也知道 hexdigest 部分,并且在尝试模仿该行为时牢记这一点。
有谁知道如何在 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();
}
}
此代码取自:Hashing with SHA1 Algorithm in C#
另一种方式
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 and C# hmac
编辑 2
一些 input/output:
C#(使用上面描述中提供的第二种方法)
输入: callerId1495610997apiKey3*_&E#N@B1)O)-1Y
输出:1ecded2b66e152f0965adb96727d96b8f5db588a
Python
输入: callerId1495610997apiKey3*_&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 并使用相同的大小写。输出是一样的。
请注意 在您的输入字符串中(在 callerId1495611935
和 apiKey{[B{+%P)s;WD5&5x
之间)有一个 hidden \u200b
character。这就是为什么用 UTF-8 编码字符串会产生与使用 ASCII 编码不同的结果。该字符是否必须在您的字符串中?
我有一个字符串需要散列才能访问 API。 API-creator 在 Python 中提供了一个代码片段,它对代码进行了哈希处理,如下所示:
hashed_string = hashlib.sha1(string_to_hash).hexdigest()
当使用这个散列字符串访问 API 时,一切正常。我试图在 C# 中获得相同的散列字符串结果,但没有成功。我已经尝试了很多方法,但到目前为止没有任何效果。我也知道 hexdigest 部分,并且在尝试模仿该行为时牢记这一点。
有谁知道如何在 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();
}
}
此代码取自:Hashing with SHA1 Algorithm in C#
另一种方式
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 and C# hmac
编辑 2
一些 input/output:
C#(使用上面描述中提供的第二种方法)
输入: callerId1495610997apiKey3*_&E#N@B1)O)-1Y
输出:1ecded2b66e152f0965adb96727d96b8f5db588a
Python
输入: callerId1495610997apiKey3*_&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 并使用相同的大小写。输出是一样的。
请注意 在您的输入字符串中(在 callerId1495611935
和 apiKey{[B{+%P)s;WD5&5x
之间)有一个 hidden \u200b
character。这就是为什么用 UTF-8 编码字符串会产生与使用 ASCII 编码不同的结果。该字符是否必须在您的字符串中?