Convert String to MD5 Hash

mintz

Board Regular
Joined
Aug 5, 2015
Messages
117
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



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

jamcall

Active Member
Joined
Sep 19, 2013
Messages
287
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:

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,732
Hi mintz

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

mintz

Board Regular
Joined
Aug 5, 2015
Messages
117
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
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,732
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:

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,732
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
 

mintz

Board Regular
Joined
Aug 5, 2015
Messages
117
Wonderful works like a charm! Thank you guys!
 

JLalonde

New Member
Joined
Oct 22, 2019
Messages
10
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
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,732
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.
 

Forum statistics

Threads
1,077,822
Messages
5,336,564
Members
399,088
Latest member
Swindlestikz

Some videos you may like

This Week's Hot Topics

Top