How to convert as string into MD5 Hash



  • Hope you all are doing well. Recently I was trying to convert a string into an md5 hash but I'm unable to do so. I have also tried to copy OpenBullet md5 has function here it is

     Public Function encryptToMD5(input As Byte()) As Byte()
            Dim m As MD5 = System.Security.Cryptography.MD5.Create
            Return m.ComputeHash(input)
        End Function
    

    But when I passed this string "952b4412f002315aa50751032fcaab03" & currentTimeInUnix it is showing me error that long can not be converted to byte. kindly help me if you know the solution.


  • Admin

    If you want a string to string hashing you have to do something like this
    https://stackoverflow.com/questions/23513831/hash-with-md5-in-vb-net



  • @Ruri I have already used this function

    Shared Function GetHash(theInput As String) As String
    
        Using hasher As MD5 = MD5.Create()    ' create hash object
    
            ' Convert to byte array and get hash
            Dim dbytes As Byte() = 
                 hasher.ComputeHash(Encoding.UTF8.GetBytes(theInput))
    
            ' sb to create string from bytes
            Dim sBuilder As New StringBuilder()
    
            ' convert byte data to hex string
            For n As Integer = 0 To dbytes.Length - 1
                sBuilder.Append(dbytes(n).ToString("X2"))
            Next n
    
            Return sBuilder.ToString()
        End Using
    
    End Function
    

    But the result isn't the same as OB that's why I think it's not giving me the right hash.
    it looks like this

    C269A4B21346CEFCA0D101858E74EFC7
    

    but in OB it same string look like this

    293eee0234abb560881101050b97ba31
    

  • Admin

    It should work exactly the same. Anyways OB's function wants a byte array and outputs a byte array so you need to convert using Encoding.UTF8.GetBytes(yourString) and then pass it to the function, which will return another byte array. Then you need to convert it again to a hex string (I made a ToHex() string extension)



  • @Ruri
    I have made that function that you used in OB

    Public Function toHex(bytes As Byte()) As String
            Dim sb = New StringBuilder(bytes.Length * 2)
            For Each b As Byte In bytes
                sb.Append(b.ToString("X2"))
            Next
            Return sb.ToString()
        End Function
    

    And here how I'm using this

     Dim currentTimeInUnix = ToUnixTimeSeconds(DateTime.Now.ToString("yyyy-MM-dd hh:mm:dd"))
                    Dim baseString As String = "952b4412f002315aa50751032fcaab03" & currentTimeInUnix
                    Dim rawInput = Encoding.UTF8.GetBytes(baseString)
                    Dim digest As Byte() = encryptToMD5(rawInput)
                    TextBox3.Text = toHex(digest)
    

    But when I use this hash in OB it says invalid hash that's mean both hashes are different.
    Kindly help me I'm stuck here



  • @King_Kong
    Hey mate here is a piece of code that you could use to convert string To MD5 Hash I didn't made it I just did the homework for you

        Sub Main()
            Dim StrToHash As String
            StrToHash = "MyStringToHash"
            Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
            Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(StrToHash) 
            bytesToHash = md5Obj.ComputeHash(bytesToHash) 
            Dim strResult As String = "" 
            For Each b As Byte In bytesToHash
                StrResult += b.ToString("x2")
            Next 
            Console.Write(StrResult)
        End Sub
    

    String To Hash MD5.PNG

    String To Hash MD5 OB.PNG


  • Admin

    @Br4uN thanks, nice job



  • @Br4uN Thanks man I really appreciate that and @Ruri Thanks for your help


Log in to reply