[英]SHA256 hashing a value in Python and VB.NET produce different hashes
我试图在以前用VB.NET编写的Python中重新实现哈希函数。 该函数接受一个字符串并返回哈希值。 哈希然后存储在数据库中。
Public Function computeHash(ByVal source As String)
If source = "" Then
Return ""
End If
Dim sourceBytes = ASCIIEncoding.ASCII.GetBytes(source)
Dim SHA256Obj As New Security.Cryptography.SHA256CryptoServiceProvider
Dim byteHash = SHA256Obj.ComputeHash(sourceBytes)
Dim result As String = ""
For Each b As Byte In byteHash
result += b.ToString("x2")
Next
Return result
返回61ba4908431dfec539e7619d472d7910aaac83c3882a54892898cbec2bbdfa8c
我的Python重新实现:
def computehash(source):
if source == "":
return ""
m = hashlib.sha256()
m.update(str(source).encode('ascii'))
return m.hexdigest()
返回e33110e0f494d2cf47700bd204e18f65a48817b9c40113771bf85cc69d04b2d8
相同的十个字符串用作两个功能的输入。
谁能说出为什么函数返回不同的哈希值?
我使用Microsoft的示例在TIO上创建Visual Basic .Net实现。 它在功能上等同于您的实现,并且我验证了您的字符串生成器会产生与我不熟悉VB相同的输出。
TIO上类似的python 3实现产生的哈希与上述VB示例相同。
import hashlib
m = hashlib.sha256()
binSrc = "abcdefghij".encode('ascii')
# to verify the bytes
[print("{:2X}".format(c), end="") for c in binSrc]
print()
m.update(binSrc)
print(m.hexdigest())
因此,我无法重现您的问题。 如果您创建了一个最小,完整和可验证的示例 ,也许我们可以为您提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.