如何使用 python 和 unity3d C# 比较图像的哈希值?

How to compare hash of an image using python and unity3d C#?

我有一个 unity3d 应用程序请求 json 图像名称字符串,包括它在我的 django 网络服务器中的哈希值。然后我的统一应用程序将检查我现有的图像哈希,如果它与请求的 json 相同。我的问题是统一哈希结果与我的 python 哈希结果值不同。我还尝试在两者上对字符串进行哈希处理,它 returns 具有相同的哈希值。

Python 哈希:

>>> image_file = open('C:/image.png').read()
>>> hashlib.md5(image_file).hexdigest()
'658e8dc0bf8b9a09b36994abf9242099'

Unity3d 哈希:

public static string ComputeHash()
{
    // Form hash
    System.Security.Cryptography.MD5 h =System.Security.Cryptography.MD5.Create();
    var myImage = File.OpenRead(PathByPlatform("image.png"));
    var data = h.ComputeHash(myImage );

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    for (int i = 0; i < data.Length; ++i)
    {
        sb.Append(data[i].ToString("x2"));
    }
    return sb.ToString();

    //This fucntion returns 
    //fac7f19792a696d81be77aca7dd499d0
}

您是否尝试 open('C:/image.png', "rb").read() 以二进制模式读取文件?

读取没有 "b" 的文件会将 Windows 上的行结束字符从 CR/LF 更改为 LF,这会对散列产生影响。 (至少 python2)