【发布时间】:2018-03-09 15:09:49
【问题描述】:
我正在尝试在 Python 中重新实现以前用 VB.NET 编写的散列函数。该函数接受一个字符串并返回哈希值。然后将哈希值存储在数据库中。
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
两个函数都使用相同的十个字符串作为输入。
谁能说出为什么这些函数返回不同的哈希值?
【问题讨论】:
-
查看字节数。可能只是编码不同
-
可能从散列一个空字符串(需要更改您的 VB.NET 代码)或单个字节开始以缩小问题范围。
标签: python vb.net hash sha256 hashlib