Thanks for sharing the MD5() function. However, I'm having an issue it when the string to hash includes 8-bit (ASCII Extended) or UTF-8 Unicode chars. I'm running this script with AHK 1.1.30.03 64-bits.
Code: Select all
obj := Object()
; single arrays with a string to encode, and its expected MD5 hash
obj["ASCII 7-bit"] := ["-a-", "9ad117707c4c3bf4fa6f134bd3388806"]
obj["ASCII 8-bit"] := ["-" . Chr(224) . "-", "25719c185c8731f9d03fffa93cbb88fb"] ; "-à-"
obj["ASCII UTF-8"] := ["-" . Chr(257) . "-", "8ff009cf903b029ead4b37f2b530ba60"] ; "-ā-" (a with a small bar on its top)
for key, arr in obj
str .= arr[1] . " (" . key . ") is " . (MD5(arr[1]) = arr[2] ? "GOOD" : "different") . "`n"
MsgBox, % SubStr(str, 1, -1) ; remove last `n
ExitApp
;------------------------------------------------------------
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)
}
;------------------------------------------------------------
The obj object includes arrays with 1) examples of strings and 2) the expected results based on the result from various online MD5 hash generators (one of them being
http://www.md5.cz/). Only the first array with plain 7-bit ASCII produces the expected result.
I'm also using the following VBA function in Excel that produces the expected result for the three values. I need to be able to match the results in XL with those produced in AHK. I'm not enough fluent with bits-and-bytes to solve this alone. Any help would be appreciated.
Code: Select all
' from https://www.mrexcel.com/forum/excel-questions/973381-convert-string-md5-hash.html
Function StringToMD5HexUTF8(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
StringToMD5HexUTF8 = 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