Solved

Comparing MD5 Hashed strings

Posted on 2004-09-10
2
1,067 Views
Last Modified: 2009-12-16
Hi,

I have some trouble comparing hashed strings between .Net and .Net Compact Framework. I used normal VB.NET to store a MD5 hashed string in a database. I used the following code to create and compare the same hash (at least, that is what i thought) using VB.NET Compact Framework (with a little help from OpenNetCF)

Dim clearBytes As [Byte]()
Dim MD5 As New MD5CryptoServiceProvider
Dim hashedText As String
Dim hashedBytes As [Byte]()
Dim str as string = "123"

clearBytes = Encoding.Unicode.GetBytes(str)
hashedBytes = MD5.ComputeHash(clearBytes)
hashedText = BitConverter.ToString(hashedBytes)

This produces the following Hash:

"5F-A2-85-E1-BE-BE-A-66-23-E3-3A-FC-4-A1-FB-D5"

If I compare this to the same Hash i did with VB.NET (Using standard .Net Crypto Provider):

"5F-A2-85-E1-BE-BE-0A-66-23-E3-3A-FC-04-A1-FB-D5"

there is a difference.

Does anyone know how to fix this issue?

BR, DM
0
Comment
Question by:Darq_Messiah
  • 2
2 Comments
 
LVL 3

Expert Comment

by:jetforce
ID: 12024882
the problem is is the value is less than 15 (hex F) then there is no prefix of 0
                              V                         V
"5F-A2-85-E1-BE-BE-A-66-23-E3-3A-FC-4-A1-FB-D5"

If I compare this to the same Hash i did with VB.NET (Using standard .Net Crypto Provider):

"5F-A2-85-E1-BE-BE-0A-66-23-E3-3A-FC-04-A1-FB-D5"


There is two ways to do this
1)  find all the single figures and prefix them with a 0 and then do the compare

or split the string into an array by using the split variable as '-'
2)  then compare the values individually by converting them to an integer

Jetforce
0
 
LVL 3

Accepted Solution

by:
jetforce earned 250 total points
ID: 12024916

      MsgBox(ConvertHashProp("5F-A2-85-E1-BE-BE-A-66-23-E3-3A-FC-4-A1-FB-D5"))


    Private Function ConvertHashProp(ByVal value As String) As String
        Dim strArr() As String = Split(value, "-")
        For i As Int16 = 0 To strArr.GetUpperBound(0) - 1
            If strArr(i).Length = 1 Then
                strArr(i) = "0" & strArr(i)
            End If
        Next

        Return Join(strArr, "-")
    End Function
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question