VB.Net控制台应用程序返回SHA512哈希

时间:2011-01-24 05:44:58

标签: vb.net access-vba

我正在尝试在VB.Net(2008)中编写一个控制台应用程序来接受命令行参数输入(密码)并输出哈希值。我修改了the code here以便它接受一个参数并且我添加了我自己的salt / key但是与PHP输出相同的密码和salt相比,输出似乎是不正确的。

我需要这样的东西,所以我将有办法使用MS Access VBA获取SHA512哈希。你能推荐一个比我正在尝试的更好的解决方案,还是指出比上面的链接更简单的东西?我可以在这里发布我的代码,但它很长。

EDIT1: 我现在正在使用Smerkin Gherkin的代码,但我的PHP哈希和我的VB.Net哈希仍然不匹配。我正在使用的密码和盐(仅作为测试)是:Password1 abcd1234

我的VB.Net代码如下所示: SHA512Hasher.HexHash(“Password1”,“abcd1234”)

我的PHP代码如下所示: echo hash_hmac(“sha512”,“Password1”,“abcd1234”);

VB.Net返回此信息: 4E1112E5D2995ECDBF5777BA2B5425B86164044B1564D4A2E0232F5E5BC4A4DA34E9AD8C2E5F664FE795C5ED12753B56563164F239070C45B8278F268A8A0860

PHP返回: 46d338722b931f2e025ecaa7853da070ad74a4e648c48633388740d647f1edba7f83afe43a104d7f7e0662e130a184743caea39177bc8deda030087d9e425e09

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:3)

最简单的选择是使用System.Security.Cryptography.Sha512Managed对象。下面是一个将SHA512哈希值作为base64字符串或十六进制字符串返回的示例。

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函数使用一个键来散列值,该键与盐化哈希值(使用salt值前缀明文)不同。要使用的.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

我还更改了十六进制函数以返回小写散列以匹配php结果。