Convert String to MD5 Hash

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
I have this string
71140553442112312345ABCDE

I want to convert it to MD5 Hash
efd49dfb22292d6c42c0941f08cc4717

I found this function that converts takes the string from a file but in my case the string is stored in a variable

7755f2b4f7.png


How do I edit the function so the input is a String rather than a File?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Your function doesn't display for me, but there is a post on <a href='http://<a href="http://stackoverflow.com/questions/125785/password-hash-function-for-excel-vba" target="_blank">http://stackoverflow.com/questions/125785/password-hash-function-for-excel-vba</a>' target="_blank">Password hash function for Excel VBA - Stack Overflow that implements MD5 Hash in an Excel VBA module...

Cheers,
~ Jim
 
Last edited:
Upvote 0
Hi mintz

The conversion seems simple. Can you post the text of the code so that we can test?
 
Last edited:
Upvote 0
Hi mintz

The conversion seems simple. Can you post the text of the code so that we can test?
Hi, here's the code in text:
Code:
Private Sub TestMD5()
    Debug.Print FileToMD5Hex("C:\test.txt")
End Sub


Public Function FileToMD5Hex(toMD5 As String) As String
    Dim enc
    Dim bytes
    Dim outstr As String
    Dim pos As Integer
    Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    'Convert the string to a byte array and hash it
    bytes = GetFileBytes(sFileName)
    bytes = enc.ComputeHash_2((bytes))
    'Convert the byte array to a hex string
    For pos = 1 To LenB(bytes)
        outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
    Next
    FileToMD5Hex = outstr
    Set enc = Nothing
End Function


Private Function GetFileBytes(ByVal path As String) As Byte()
    Dim lngFileNum As Long
    Dim bytRtnVal() As Byte
    lngFileNum = FreeFile
    If LenB(Dir(path)) Then ''// Does file exist?
        Open path For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise 53
    End If
    GetFileBytes = bytRtnVal
    Erase bytRtnVal
End Function
 
Upvote 0
Seems much simpler with a string.

Try:

Code:
Sub TestMD5()
Dim s As String

    s = "71140553442112312345ABCDE"
    Debug.Print StringToMD5Hex(s)

End Sub

Function StringToMD5Hex(ByVal s As String) As String
Dim enc As Object
Dim bytes() As Byte
Dim pos As Long
Dim outstr As String

Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

bytes = StrConv(s, vbFromUnicode)
bytes = enc.ComputeHash_2(bytes)

For pos = 1 To UBound(bytes) + 1
   outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next pos

StringToMD5Hex = outstr
Set enc = Nothing
End Function


Remark:
I did not study the object you used, just changed the vba syntax.
 
Last edited:
Upvote 0
The loop can be simpler and more efficient:

Code:
For pos = LBound(bytes) To UBound(bytes)
   outstr = outstr & LCase(Right("0" & Hex(bytes(pos)), 2))
Next pos
 
Upvote 0
Hi,

I reactivate this old thread. Thanks for the StringToMD5Hex function code. Very helpful. However, I found that it does not support Unicode characters. For example for "Doğan" (with an accent over the "g"), the MD5 returned by this VBA function is not the same as one produced in another environment supporting Unicode characters.

If one of the experts here could help me make this work?

Thanks for your help,

Jean
 
Upvote 0
Hi

Like I said I don't know the object, but maybe you can post some examples with input and expect output strings. This way, someone that wants to try it has a way to check the results.
 
Upvote 0

Forum statistics

Threads
1,223,219
Messages
6,170,803
Members
452,353
Latest member
ledowning

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top