最简单的选择是使用System.Security.Cryptography.Sha512Managed
目的。下面的示例将以 base64 字符串或十六进制字符串的形式返回 SHA512 哈希值。
Public Class SHA512Hasher
Private Sub New()
' Prevent instantiation
End Sub
Public Shared Function Base64Hash(ByVal clearText As String) As String
Dim hashedBytes As Byte() = computeHash(clearText)
Return Convert.ToBase64String(hashedBytes)
End Function
Public Shared Function Base64Hash(ByVal clearText As String, ByVal salt As String) As String
Return Base64Hash(salt & clearText)
End Function
Public Shared Function HexHash(ByVal clearText As String) As String
Dim hashedBytes As Byte() = computeHash(clearText)
' Build the hex string by converting each byte.
Dim hexString As New System.Text.StringBuilder()
For i As Int32 = 0 To hashedBytes.Length - 1
hexString.Append(hashedBytes(i).ToString("X2")) ' Use "x2" for lower case
Next
Return hexString.ToString()
End Function
Public Shared Function HexHash(ByVal clearText As String, ByVal salt As String) As String
Return HexHash(salt & clearText)
End Function
Private Shared Function computeHash(ByVal clearText As String) As Byte()
Dim encoder As New Text.UTF8Encoding()
Dim sha512hasher As New System.Security.Cryptography.SHA512Managed()
Return sha512hasher.ComputeHash(encoder.GetBytes(clearText))
End Function
End Class
用于将一些输出打印到命令行的简单控制台应用程序是
Module Module1
Sub Main()
Dim clear As String = "Foo"
Dim salt As String = "Salted"
Console.WriteLine(SHA512Hasher.Base64Hash(clear))
Console.WriteLine(SHA512Hasher.Base64Hash(clear, salt))
Console.WriteLine(SHA512Hasher.HexHash(clear))
Console.WriteLine(SHA512Hasher.HexHash(clear, salt))
End Sub
End Module
Edit1 - 更新以响应有问题的 Edit1
php 函数使用一个键对值进行哈希处理,这与对哈希值加盐(在明文前面加上盐值作为前缀)不同。要使用的 .Net 对象是System.Security.Cryptography.HMACSHA512
。上述代码的更新版本是:
Public Class HMACSHA512Hasher
Private Sub New()
' Prevent instantiation
End Sub
Public Shared Function Base64Hash(ByVal clearText As String) As String
Return Base64Hash(clearText, String.Empty)
End Function
Public Shared Function Base64Hash(ByVal clearText As String, ByVal key As String) As String
Dim hashedBytes As Byte() = computeHash(clearText, key)
Return Convert.ToBase64String(hashedBytes)
End Function
Public Shared Function HexHash(ByVal clearText As String) As String
Return HexHash(clearText, String.Empty)
End Function
Public Shared Function HexHash(ByVal clearText As String, ByVal key As String) As String
Dim hashedBytes As Byte() = computeHash(clearText, key)
' Build the hex string by converting each byte.
Dim hexString As New System.Text.StringBuilder()
For i As Int32 = 0 To hashedBytes.Length - 1
hexString.Append(hashedBytes(i).ToString("x2")) ' Use "x2" for lower case
Next
Return hexString.ToString()
End Function
Private Shared Function computeHash(ByVal clearText As String, ByVal key As String) As Byte()
Dim encoder As New Text.UTF8Encoding()
Dim sha512hasher As New System.Security.Cryptography.HMACSHA512(encoder.GetBytes(key))
Return sha512hasher.ComputeHash(encoder.GetBytes(clearText))
End Function
End Class
我还更改了 hex 函数以返回小写哈希以匹配 php 结果。