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?
 
When I encode this one letter Unicode string "ğ" (Unicode ASCII 287) with the function StringToMD5Hex (source earlier in this thread), it returns the following value:

Code:
=StringToMD5Hex(UNICAR(287))
Result: b2f5ff47436671b6e533d8dc3614845d

When I encode the same string with this AHK script:

Code:
str := Chr(287)
MsgBox, % MD5(str)

;------------------------------------------------------------
MD5(str, blnCase := false)
; by SKAN | rewritten by jNizM (https://www.autohotkey.com/boards/viewtopic.php?f=76&t=14927&p=75925&hilit=MD5sum#p75944)
;------------------------------------------------------------
{
    static MD5_DIGEST_LENGTH := 16
    hModule := DllCall("LoadLibrary", "Str", "advapi32.dll", "Ptr")
        , VarSetCapacity(MD5_CTX, 104, 0), DllCall("advapi32\MD5Init", "Ptr", &MD5_CTX)
        , DllCall("advapi32\MD5Update", "Ptr", &MD5_CTX, "AStr", str, "UInt", StrLen(str))
        , DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
    loop % MD5_DIGEST_LENGTH
        o .= Format("{:02" (blnCase ? "X" : "x") "}", NumGet(MD5_CTX, 87 + A_Index, "UChar"))
    return o, DllCall("FreeLibrary", "Ptr", hModule)
}
;------------------------------------------------------------

I get a different result:
d1457b72c3fb323a2671125aef3eab5d

Doing test with this script, I found that when it is running without Unicode support, it produces the same result as the VBA function StringToMD5Hex. This takes me to the conclusion that the VBA function does not support Unicode encoding. I don't have the knowledge to adapt this function to Unicode. If someone could work on it, I would apprecitate.

Thanks,

Jean
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Change

Code:
bytes = StrConv(s, vbFromUnicode)

to

Code:
bytes = s

That avoids killing the Unicode.
 
Upvote 0
Thanks shg. I tried this.

Code:
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 = s
    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

But this produces a 3rd result:
f57daa74a09445d1e1c496f28fe6d906

instead of (with unchanged StringToMD5Hex):
b2f5ff47436671b6e533d8dc3614845d


The result I think I should get is (based on AHK script):
d1457b72c3fb323a2671125aef3eab5d
 
Upvote 0
Last edited:
Upvote 0
Thanks for these resources, pgc.

The AHK script was found on AHK forum (https://www.autohotkey.com/boards/viewtopic.php?f=76&t=14927&p=75925&hilit=MD5sum#p75944). I could not explain its logic and would not be able to change it.

As I mentioned, I deduced that this AHK script supported Unicode but I could not be sure. And with the result you got from these 5 sites, I'm not sure at all... And I could not find confirmation that these tools truly support Unicode (Unicode support is not always a given).

Anyway, if someone could help me with this. Again, I'd like to find AHK and VBA scripts that would return the correct value for Unicode strings. If anybody can help.

Thanks again pcpg for your input.
 
Upvote 0
... And with the result you got from these 5 sites, I'm not sure at all... And I could not find confirmation that these tools truly support Unicode (Unicode support is not always a given).

Well, I don't have much time now to test, but I just tried with the code in this thread for the character you want (character 287: ğ) and it worked with no problem.
I used the usual utf8 codification for the character: HC4 H9F

Just for testing I hardcoded the values in the function, that you can 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)
' test for character 287 = HC4 H9F
ReDim bytes(0 To 1) As Byte
bytes(0) = &HC4
bytes(1) = &H9F
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

As you can see you get the expected 1f4707e216e56f3385d7be592b694bf6 that you get from all those sites.

As I said I don't really know md5, but the result for the character you posted is OK.
Some test needed, but what I understood is that md5 doesn't care about ascii or unicode, it just processes a sequence of bits.
It's up to you to input those bytes. In the case of ascii you enter them directly. From the test I just made it seems that the usual for unicode is to use utf8.
You can, of course, use other encoding, that's irrelevant to the md5 algorithm. YOu just must make sure that everyone knows your choice.
Give that (it seems) the usual in the online hash generation is utf8, that may be a good choice.
Test and choose.
 
Upvote 0
I had a little time now and did some testing.

I tried to get the code to return the same result as what the online md5 encoders return.

To test I used:
Some japanese: "お名前は?" "(What's your) name?".
Some hindi: "मानक हिन्दी" :Devanagari
and something more familiar, English (Chaucer) :)
"And wol nat suffren hem, by noon assen"

I concatenated the 3:
お名前は?मानक हिन्दीAnd wol nat suffren hem, by noon assen
and wrote it in A1

I got the same result running the code and using the 5 online sites that I posted before.
765e47e5228aac498acae4a2c562e144

This tells me that it seems I'm in the right track.

The code simply encodes the string in utf8 and then generates the hash.

I just added some code to do the utf8 encoding.
I could write a vba snippet since it's a simple algorithm, but I preferred using the encoding available with ms ado.

To test, just write strings in A1 and compare the result with the one you get from the online md5 encoders (for ex., use the 5 that I posted).
If you get different results, please post them.

Here is the code I used:


Code:
Sub TestMD5()
Dim s As String

    s = Range("A1").Value
    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 = UTF8Encoder(s)
bytes = enc.ComputeHash_2(bytes)

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

StringToMD5Hex = outstr
Set enc = Nothing
End Function

' encode a string as utf8, set reference to Microsoft ActiveX Data Objects 6.1 Library
Function UTF8Encoder(s As String) As Byte()
Dim objStream As ADODB.Stream

Set objStream = New ADODB.Stream
objStream.Charset = "utf-8"
objStream.Open
objStream.WriteText s
objStream.Flush
objStream.Position = 0
objStream.Type = adTypeBinary
objStream.Position = 3 'no bom
UTF8Encoder = objStream.Read
objStream.Close
Set objStream = Nothing
End Function
 
Last edited:
Upvote 0
Hi

You just missed it in the code of the encoding function:

Code:
' encode a string as utf8, set reference to Microsoft ActiveX Data Objects 6.1 Library
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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