Results 1 to 3 of 3

Thread: A VB.NET equivalent of a PHP hash function

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018

    A VB.NET equivalent of a PHP hash function

    I am having trouble converting this PHP code to VB.
    PHP Code:
    $result hash('sha256'$postdatatrue
    this function can be consulted at this address http://php.net/manual/en/function.hash.php

    I have this VB code:
        Public Function SHA256(ByVal Content As String, ByVal raw_output As Boolean) As String
            Dim MoLeCuL3 As New Security.Cryptography.SHA256CryptoServiceProvider
            Dim ByteString() As Byte = System.Text.Encoding.ASCII.GetBytes(Content)
            ByteString = MoLeCuL3.ComputeHash(ByteString)
            Dim FinalString As String = Nothing
            For Each bt As Byte In ByteString
                If raw_output Then
                    '..... ????? ..........
                    FinalString &= bt.ToString("x2")
                End If
            Return FinalString
        End Function
    but I can not code the result in "raw binary data", as does the original PHP function with the third parameter set to TRUE.
    Can anyone help me with this? Very much appreciated. Thank you.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: A VB.NET equivalent of a PHP hash function

    Hashing is performed on Byte arrays. The Byte array is the "raw binary data". Where the source Byte array comes from (file, text, Image, etc) is irrelevant to the hashing process, as is where the resultant Byte array goes to. In your VB code, ByteString (which is a poor name) contains the raw binary data that you want.

    You can't really have a single method that returns either a String or a Byte array unless you declare the return type as Object, which is a fairly bad idea. I'd suggest two methods: one that returns a String and one that returns a Byte array. The first of those would call the second and convert the Byte array to a hex string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    You don't want to know.
    Join Date
    Aug 2010

    Re: A VB.NET equivalent of a PHP hash function

    Yeah, what you're asking for is considered bad practice in VB. PHP is a loosely-typed dynamic language and, for years, was designed by monkeys, so the convention is to return many different types from functions and to not always document every case. In VB .NET, you have to up-front declare your return type and write code that provably returns that type.

    So in VB .NET, I'd expect an API surface like this:
    Function GetSHA256(...) As Byte()
        ' <do the things to get the byte array>
    End Function
    Function GetSHA256AsString(...) As String
        Dim bytes() As Byte = GetSHA256(...)
        ' <do the things to convert the byte array to a String>
    End Function
    There are a couple of other ways to make it nice, but that is the simplest approach. You could, for example, make a type to help out:
    Public Class SHA256Data
        Public Property Hash() As Byte()
        Public Property HashString() As String
    End Class
    Then you can write one function that returns both things:
    Function GetSHA256(...) As SHA256Data
        Dim hashBytes() As Byte
        ' <do the thing to set hashBytes to the proper hash bytes>
        Dim hashString As String
        ' <do the thing to set hashString to the right String
        Return New SHA256Data() With {
            .Hash = hashBytes,
            .HashString = hashString
    End Function
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width