需要帮助将C#“字节数学”转换为Python

我们有一个古老的,自定义的C#哈希算法,可用于为PII目的屏蔽电子邮件地址。我正在尝试构建该算法的Python版本,但是我正在努力处理C#和Python处理字节/字节数组的方式上的差异,从而产生错误的哈希值。作为参考,这是Python 2.7,但是Python 3+解决方案也可以工作。

C#代码:

using System.Text;
using System.Security;
using System.Security.Cryptography;

public class Program
{
    public static void Main()
    {
        string emailAddressStr = "my@email.com";
        emailAddressStr = emailAddressStr.Trim().ToLower();
        SHA256 objCrypt = new SHA256Managed();
        byte[] b = (new ASCIIEncoding()).GetBytes(emailAddressStr);
        byte[] bRet = objCrypt.ComputeHash(b);

        string retStr = "";
        byte c;

        for (int i = 0; i < bRet.Length; i++)
        {
            c = (byte)bRet[i];
            retStr += ((char)(c / 10 + 97)).ToString().ToLower();
            retStr += ((char)(c % 10 + 97)).ToString().ToLower();
        }
        Console.WriteLine(retStr);
    }
}

The (correct) value that gets returned is uhgbnaijlgchcfqcrgpicdvczapepbtifiwagitbecjfqalhufudieofyfdhzera

Python翻译:

import hashlib

emltst = "my@email.com"

emltst = emltst.strip().lower()
b = bytearray(bytes(emltst).encode("ascii"))
bRet = bytearray(bytes(hashlib.sha256(b)))

emailhash=""

for i in bRet:
    c = bytes(i)
    emailhash = emailhash + str(chr((i / 10) + 97)).lower()
    emailhash = emailhash + str(chr((i % 10) + 97)).lower()

print(emailhash)

The (incorrect) value I get here is galfkejhfafdfedchcgfidhcdclbjikgkbjjlgdcgedceimaejeifakajhfekceifggc

The "business end" of the code is in the loop where c is not translating well between languages. C# produces a numeric value for the calculation, but in Python, c is a string (so I'm using i). I've stepped through both sets of code and I know that I'm producing the same hash value right before the loop. I hope someone here might be able help me out. TIA!

评论
  • mut
    mut 回复

    你去了(python 3.0)

    笔记:

    1. hashAlgorithm.update需要编码的字符串,因此为b“ my@email.com”
    2. chr((i / 10)+ 97失败,显示“ expect int found float”,因此//
    import hashlib
    
    emltst = b"my@email.com"
    
    emltst = emltst.strip().lower()
    
    hashAlgorithm = hashlib.sha256()
    hashAlgorithm.update(emltst)
    # Thanks to Mark Meyer for pointing out.
    # bytearray(bytes( are redundant
    bRet = hashAlgorithm.digest()
    
    emailhash=""
    
    for i in bRet:
        c = bytes(i)
        emailhash = emailhash + str(chr((i // 10) + 97)).lower()
        emailhash = emailhash + str(chr((i % 10) + 97)).lower()
    
    print(emailhash)
    

    输出:

    uhgbnaijlgchcfqcrgpicdvczapepbtifiwagitbecjfqalhufudieofyfdhzera