我们有一个古老的,自定义的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!
你去了(python 3.0)
笔记:
输出: