在C#中无法获得与python中相同的哈希

Mar*_*son 3 c# python hash sha1

I have a string that I need to hash in order to access an API. The API-creator has provided a codesnippet in Python, which hashes the code like this:

hashed_string = hashlib.sha1(string_to_hash).hexdigest()
Run Code Online (Sandbox Code Playgroud)

When using this hashed string to access the API, everything is fine. I have tried to get the same hashed string result in C#, but without success. I have tried incredibly many ways but nothing has worked so far. I am aware about the hexdigest part aswell and I have kept that in mind when trying to mimic the behaviour.

Does anyone know how to get the same result in C#?

EDIT: This is one of the many ways I have tried to reproduce the same result in 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

This code is taken from: Hashing with SHA1 Algorithm in C#

Another way

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();
}
Run Code Online (Sandbox Code Playgroud)

This code is taken from: Python hmac and C# hmac

EDIT 2

Some input/output:

C#(使用上面描述中提供的第二种方法)

输入:callerId1495610997apiKey3 * _&E#N @ B1)O)-1Y

输出:1ecded2b66e152f0965adb96727d96b8f5db588a


蟒蛇

输入:callerId1495610997apiKey3 * _&E#N @ B1)O)-1Y

输出:bf11a12bbac84737a39152048e299fa54710d24e


C#(使用上面描述中提供的第一种方法)

输入:callerId1495611935?apiKey {[B {+%P)s; WD5&5x

输出:7e81e0d40ff83faf1173394930443654a2b39cb3


蟒蛇

输入:callerId1495611935?apiKey {[B {+%P)s; WD5&5x

输出:512158bbdbc78b1f25f67e963fefdc8b6cbcd741

Fed*_*uma 5

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  ="callerId1495611935?apiKey{[B{+%P)s;WD5&5x";
    Console.WriteLine(Hash(x)); // prints 7e81e0d40ff83faf1173394930443654a2b39cb3
}
Run Code Online (Sandbox Code Playgroud)

蟒蛇

import hashlib
s = u'callerId1495611935?apiKey{[B{+%P)s;WD5&5x'
enc = s.encode('utf-8') # encode in utf8
hash = hashlib.sha1(enc)
formatted = h.hexdigest()
print(formatted) # prints 7e81e0d40ff83faf1173394930443654a2b39cb3
Run Code Online (Sandbox Code Playgroud)

您的主要问题是,对于C#和Python中的同一字符串,您正在使用不同的编码。使用两种语言的UTF8并使用相同的大小写。输出是相同的。

请注意,在输入字符串(callerId1495611935和之间apiKey{[B{+%P)s;WD5&5x)中有一个隐藏 \u200b字符。这就是为什么用UTF-8编码字符串会产生与使用ASCII编码不同的结果的原因。该字符是否必须在字符串中?