[英]SHA512 encoding in python
I need help with regards to sha512 encoding in python.我需要有关 python 中 sha512 编码的帮助。 I am trying to write a piece of python code that should be in line with c# code.我正在尝试编写一段应符合 c# 代码的 python 代码。
Here is the method in C#这是C#中的方法
public string GenerateSHA512Hash(string data, sting salt) {
data = data.Replace(" ", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
data = data + salt;
byte[] HashedBytes = Encoding.UTF8.GetBytes(data);
using(SHA512Managed hash = new SHA512Managed()) {
for (int j = 0; j < 2; j++) {
HashedBytes = hash.ComputeHash(HashedBytes);
var text = HashedBytes.ToBase16();
}
}
return HashedBytes.ToBase16();
}
I got the following in python我在 python 中得到了以下内容
import hashlib
def HashPAN(pan: str, salt: str):
data: str = pan + salt
data = data.replace(" ", "").replace("\n", "").replace("\t", "").replace("\r", "")
data_bytes = data.encode("utf-8")
hasher = hashlib.sha512()
# First Iteration
hasher.update(data_bytes)
hashed = hasher.digest()
h = hasher.hexdigest().upper()
# Second Iteration
hasher.update(hashed)
hashed = hasher.digest()
h = hasher.hexdigest().upper()
return hashed
In python, the results from section labeled #First Iteration matches the result from the first time in the loop in C# code (h = text).在 Python 中,标记为 #First Iteration 的部分的结果与 C# 代码(h = text)中循环中第一次的结果相匹配。
However, the second time in python does not match the second time in c#.但是,python 中的第二次与 c# 中的第二次不匹配。 can someone please assist有人可以帮忙吗
I figured out how to do this in python我想出了如何在 python 中做到这一点
def HashPAN(pan: str, salt: str):
data: str = pan + salt
data = data.replace(" ", "").replace("\n", "").replace("\t", "").replace("\r", "")
data_bytes = data.encode("utf-8")
hashed_text: str
for i in range(2):
hasher = hashlib.sha512()
hasher.update(data_bytes)
data_bytes = hasher.digest()
hashed_text = hasher.hexdigest()
return hashed_text.upper()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.