Please help to convert V1 MD5 to V2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Please help to convert V1 MD5 to V2

Post by viv » 20 Apr 2021, 16:22

Thank you so much for your help!

https://www.autohotkey.com/boards/viewtopic.php?p=75944#p75944

Code: Select all

MsgBox % MD5("aa") "`n" MD5("aa", True)
 
MD5(string, case := False)    ; by SKAN | rewritten by jNizM
{
    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", string, "UInt", StrLen(string))
    , DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
    loop % MD5_DIGEST_LENGTH
        o .= Format("{:02" (case ? "X" : "x") "}", NumGet(MD5_CTX, 87 + A_Index, "UChar"))
    return o, DllCall("FreeLibrary", "Ptr", hModule)
} ;https://autohotkey.com/boards/viewtopic.php?f

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Please help to convert V1 MD5 to V2

Post by viv » 20 Apr 2021, 23:15

I still don't understand DllCall
Followed the original help I was given and changed it step by step
I don't know if this is correct or wrong
https://www.autohotkey.com/boards/viewtopic.php?f=82&t=87220#p383879

Code: Select all

MsgBox  MD5("aa") "`n" MD5("aa", True)

MD5(string, cases := False)
{
    hModule := DllCall("LoadLibrary", "Str", "advapi32.dll", "Ptr")
    MD5_CTX := BufferAlloc(104,0)
    DllCall("advapi32\MD5Init", "Ptr", MD5_CTX)
    DllCall("advapi32\MD5Update", "Ptr", MD5_CTX, "AStr", string, "UInt", StrLen(string))
    DllCall("advapi32\MD5Final", "Ptr", MD5_CTX)
    loop 16
        o .= Format("{:02" (cases ? "X" : "x") "}", NumGet(MD5_CTX, 87 + A_Index, "UChar"))
    DllCall("FreeLibrary", "Ptr", hModule)
    return o
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Please help to convert V1 MD5 to V2  Topic is solved

Post by swagfag » 21 Apr 2021, 06:33

Code: Select all

#Requires AutoHotkey v2.0-a132-8673ba2f

MsgBox  MD5("aa") "`n" MD5("aa", True)

MD5(str, cases := False)
{
    #DllLoad 'advapi32.dll'

    MD5_CTX := BufferAlloc(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)

    static MD5_DIGEST_LENGTH := 16
    o := ''
    loop MD5_DIGEST_LENGTH
        o .= Format("{:02" (cases ? "X" : "x") "}", NumGet(MD5_CTX, 87 + A_Index, "UChar"))

    return o
}
loadlibrary calls can be replaced with #DllLoad
string catenation requires prior initialization
probably best to not make ur function arguments shadow the names of classes(String), although functionally there's no difference in this case

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Please help to convert V1 MD5 to V2

Post by viv » 21 Apr 2021, 06:59

@swagfag

Thank you for your help
string may become a reserved keyword in the future like "case"?

I did notice that there is a hint in the documentation
https://lexikos.github.io/v2/docs/commands/_DllLoad.htm#Remarks
When the script is terminated, all loaded files are unloaded automatically.

Suppose my script only needs to use this function once
And my script is "Persistent"
Does it mean that the dll will not be unloaded automatically
Is "#DllLoad" better in this case?


I find this
https://github.com/Lexikos/AutoHotkey_L/pull/136#issuecomment-491742437
#loadlibrary just shouldn't call FreeLibrary and if they want to load temporary libraries, they should just do it with dllcall.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Please help to convert V1 MD5 to V2

Post by swagfag » 23 Apr 2021, 05:47

viv wrote:
21 Apr 2021, 06:59
string may become a reserved keyword in the future like "case"?
doubt it
any #DllLoaded dll is loaded and pinned(GET_MODULE_HANDLE_EX_FLAG_PIN) for as long as the script process remains alive
if ure only ever gonna call the function once and then u wish to unload it, then ur approach is fine
#DllLoad simply saves u the ceremony of having to do that urself in all other cases, thats why i suggested it

Post Reply

Return to “Ask for Help (v2)”