Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Dynamic md5 hash generation for a string



  • Please log in to reply
8 replies to this topic
andyandy
  • Members
  • 11 posts
  • Last active: May 03 2015 11:14 AM
  • Joined: 19 Feb 2014

How can I get MD5 hash to be dynamically generated inside the `Edit` field, based on the content of other `GuiEdit` field?

This script works only partially. It will generate md5 hash based on `data :=` value, but it should get it from the `GuiEdit` field. I assume I have to turn the first `GuiEdit` into a variable but I have no idea how.
     

     MD5( ByRef V, L=0 ) {
     VarSetCapacity( MD5_CTX,104,0 ), DllCall( "advapi32\MD5Init", Str,MD5_CTX )
     DllCall( "advapi32\MD5Update", Str,MD5_CTX, Str,V, UInt,L ? L : VarSetCapacity(V) )
     DllCall( "advapi32\MD5Final", Str,MD5_CTX )
     Loop % StrLen( Hex:="123456789ABCDEF0" )
      N := NumGet( MD5_CTX,87+A_Index,"Char"), MD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1)
    Return MD5
    }
    
     ; GUI =================
     
    Gui, Show, w400 h100, Md5
    
    data := %GenerateMd5Here%
    
    Gui, Add, Text, x10 y15, String:
    Gui, Add, Edit, x60 y10 w200, vVARIABLE; generate md5 based on string in this field
    
    
    Gui, Add, Text, x10 y55, Md5:
    Gui, Add, Edit,  ReadOnly Lowercase r1 x60 y50 w300, % MD5(data,StrLen(data)) ; md5


jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
; ===============================================================================================================================
; AHK Version ...: AHK_L 1.1.16.05 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: Checksum: MD5
;                  Calc MD5-Hash from String
;                  http://en.wikipedia.org/wiki/MD5
; Version .......: v1.00
; Author(s) .....: jNizM
; ===============================================================================================================================

; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force
SetBatchLines, -1

; GUI ===========================================================================================================================

Gui, Margin, 5, 5
Gui, Add, Text, xm ym w40 h21 0x200, % "String:"
Gui, Add, Edit, x+5 yp w220 gMD5GEN vGetStr,
Gui, Add, Text, xm y+8 w40 h21 0x200, % "MD5:"
Gui, Add, Edit, x+5 yp w220 0x800 gMD5GEN vCalcMD5
Gui, Show, AutoSize

MD5GEN:
    Gui, Submit, NoHide
    GuiControl,, CalcMD5, % MD5(GetStr)
return

; FUNCTION ======================================================================================================================

MD5(string, encoding = "UTF-8")
{
    return CalcStringHash(string, 0x8003, encoding)
}

CalcAddrHash(addr, length, algid, byref hash = 0, byref hashlength = 0)
{
    static h := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"]
    static b := h.minIndex()
    hProv := hHash := o := ""
    if (DllCall("advapi32\CryptAcquireContext", "Ptr*", hProv, "Ptr", 0, "Ptr", 0, "UInt", 24, "UInt", 0xf0000000))
    {
        if (DllCall("advapi32\CryptCreateHash", "Ptr", hProv, "UInt", algid, "UInt", 0, "UInt", 0, "Ptr*", hHash))
        {
            if (DllCall("advapi32\CryptHashData", "Ptr", hHash, "Ptr", addr, "UInt", length, "UInt", 0))
            {
                if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", 0, "UInt*", hashlength, "UInt", 0))
                {
                    VarSetCapacity(hash, hashlength, 0)
                    if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", &hash, "UInt*", hashlength, "UInt", 0))
                    {
                        loop % hashlength
                        {
                            v := NumGet(hash, A_Index - 1, "UChar")
                            o .= h[(v >> 4) + b] h[(v & 0xf) + b]
                        }
                    }
                }
            }
            DllCall("advapi32\CryptDestroyHash", "Ptr", hHash)
        }
        DllCall("advapi32\CryptReleaseContext", "Ptr", hProv, "UInt", 0)
    }
    return o
}

CalcStringHash(string, algid, encoding = "UTF-8", byref hash = 0, byref hashlength = 0)
{
    chrlength := (encoding = "CP1200" || encoding = "UTF-16") ? 2 : 1
    length := (StrPut(string, encoding) - 1) * chrlength
    VarSetCapacity(data, length, 0)
    StrPut(string, &data, floor(length / chrlength), encoding)
    return CalcAddrHash(&data, length, algid, hash, hashlength)
}

; EXIT ==========================================================================================================================

GuiClose:
ExitApp
for more info see here:
- Hash from string, hex or file, HMAC, Salt
- HashCalc
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

andyandy
  • Members
  • 11 posts
  • Last active: May 03 2015 11:14 AM
  • Joined: 19 Feb 2014

I have seen it, and I can't compile it. Additionally it doesn't allow dynamic md5 hash generating. Could you help me solve my problem?

 

I want to make my script work, not use another.

 

 

; ===============================================================================================================================
; AHK Version ...: AHK_L 1.1.16.05 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: Checksum: MD5
;                  Calc MD5-Hash from String
;                  http://en.wikipedia.org/wiki/MD5
; Version .......: v1.00
; Author(s) .....: jNizM
; ===============================================================================================================================

; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force
SetBatchLines, -1

; GUI ===========================================================================================================================

Gui, Margin, 5, 5
Gui, Add, Text, xm ym w40 h21 0x200, % "String:"
Gui, Add, Edit, x+5 yp w220 gMD5GEN vGetStr,
Gui, Add, Text, xm y+8 w40 h21 0x200, % "MD5:"
Gui, Add, Edit, x+5 yp w220 0x800 gMD5GEN vCalcMD5
Gui, Show, AutoSize

MD5GEN:
    Gui, Submit, NoHide
    GuiControl,, CalcMD5, % MD5(GetStr)
return

; FUNCTION ======================================================================================================================

MD5(string, encoding = "UTF-8")
{
    return CalcStringHash(string, 0x8003, encoding)
}

CalcAddrHash(addr, length, algid, byref hash = 0, byref hashlength = 0)
{
    static h := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"]
    static b := h.minIndex()
    hProv := hHash := o := ""
    if (DllCall("advapi32\CryptAcquireContext", "Ptr*", hProv, "Ptr", 0, "Ptr", 0, "UInt", 24, "UInt", 0xf0000000))
    {
        if (DllCall("advapi32\CryptCreateHash", "Ptr", hProv, "UInt", algid, "UInt", 0, "UInt", 0, "Ptr*", hHash))
        {
            if (DllCall("advapi32\CryptHashData", "Ptr", hHash, "Ptr", addr, "UInt", length, "UInt", 0))
            {
                if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", 0, "UInt*", hashlength, "UInt", 0))
                {
                    VarSetCapacity(hash, hashlength, 0)
                    if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "UInt", 2, "Ptr", &hash, "UInt*", hashlength, "UInt", 0))
                    {
                        loop % hashlength
                        {
                            v := NumGet(hash, A_Index - 1, "UChar")
                            o .= h[(v >> 4) + b] h[(v & 0xf) + b]
                        }
                    }
                }
            }
            DllCall("advapi32\CryptDestroyHash", "Ptr", hHash)
        }
        DllCall("advapi32\CryptReleaseContext", "Ptr", hProv, "UInt", 0)
    }
    return o
}

CalcStringHash(string, algid, encoding = "UTF-8", byref hash = 0, byref hashlength = 0)
{
    chrlength := (encoding = "CP1200" || encoding = "UTF-16") ? 2 : 1
    length := (StrPut(string, encoding) - 1) * chrlength
    VarSetCapacity(data, length, 0)
    StrPut(string, &data, floor(length / chrlength), encoding)
    return CalcAddrHash(&data, length, algid, hash, hashlength)
}

; EXIT ==========================================================================================================================

GuiClose:
ExitApp
for more info see here:
- Hash from string, hex or file, HMAC, Salt
- HashCalc

 



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force
SetBatchLines, -1

; GUI ===========================================================================================================================

Gui, Margin, 5, 5
Gui, Add, Text, xm ym w40 h21 0x200, % "String:"
Gui, Add, Edit, x+5 yp w220 gMD5GEN vGetStr,
Gui, Add, Text, xm y+8 w40 h21 0x200, % "MD5:"
Gui, Add, Edit, x+5 yp w220 0x800 gMD5GEN vCalcMD5
Gui, Show, AutoSize

MD5GEN:
    Gui, Submit, NoHide
    GuiControl,, CalcMD5, % MD5(GetStr)
return

; FUNCTION ======================================================================================================================

MD5(ByRef V, L := 0)
{
    local MD5_CTX := MD5 := ""
    VarSetCapacity(MD5_CTX, 104, 0), DllCall("advapi32.dll\MD5Init", "Str", MD5_CTX)
    DllCall("advapi32.dll\MD5Update", "Str", MD5_CTX, "Str", V, "UInt", L ? L : VarSetCapacity(V))
    DllCall("advapi32.dll\MD5Final", "Str", MD5_CTX)
    loop % StrLen(Hex := "123456789ABCDEF0")
        N := NumGet(MD5_CTX, 87 + A_Index, "Char"), MD5 .= SubStr(Hex, N >> 4, 1) . SubStr(Hex, N & 15, 1)
    Return MD5
}

; EXIT ==========================================================================================================================

GuiClose:
ExitApp
Or use the latest AutoHotkey Version and my Script works (with dynamic MD5 generating) for x86 (32-Bit) & x64 (64-Bit):
v1.1.16.05 - September 28, 2014
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

andyandy
  • Members
  • 11 posts
  • Last active: May 03 2015 11:14 AM
  • Joined: 19 Feb 2014

This is great! Thanks!

 

EDIT:

 

Actually it generates wrong md5 hash. Hashes are completely different when compared to for example:

http://www.adamek.bi...5-generator.php

 

For example, every time you empty the string out it gives back different hashes. Any idea what may cause this?

 

; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force
SetBatchLines, -1

; GUI ===========================================================================================================================

Gui, Margin, 5, 5
Gui, Add, Text, xm ym w40 h21 0x200, % "String:"
Gui, Add, Edit, x+5 yp w220 gMD5GEN vGetStr,
Gui, Add, Text, xm y+8 w40 h21 0x200, % "MD5:"
Gui, Add, Edit, x+5 yp w220 0x800 gMD5GEN vCalcMD5
Gui, Show, AutoSize

MD5GEN:
    Gui, Submit, NoHide
    GuiControl,, CalcMD5, % MD5(GetStr)
return

; FUNCTION ======================================================================================================================

MD5(ByRef V, L := 0)
{
    local MD5_CTX := MD5 := ""
    VarSetCapacity(MD5_CTX, 104, 0), DllCall("advapi32.dll\MD5Init", "Str", MD5_CTX)
    DllCall("advapi32.dll\MD5Update", "Str", MD5_CTX, "Str", V, "UInt", L ? L : VarSetCapacity(V))
    DllCall("advapi32.dll\MD5Final", "Str", MD5_CTX)
    loop % StrLen(Hex := "123456789ABCDEF0")
        N := NumGet(MD5_CTX, 87 + A_Index, "Char"), MD5 .= SubStr(Hex, N >> 4, 1) . SubStr(Hex, N & 15, 1)
    Return MD5
}

; EXIT ==========================================================================================================================

GuiClose:
ExitApp
Or use the latest AutoHotkey Version and my Script works (with dynamic MD5 generating) for x86 (32-Bit) & x64 (64-Bit):
v1.1.16.05 - September 28, 2014

 



andyandy
  • Members
  • 11 posts
  • Last active: May 03 2015 11:14 AM
  • Joined: 19 Feb 2014

@Bump

 

Anyone knows why does it calculates wrong hash?



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
this is the reason why you should update autohotkey and use my script in second post!
it generates the correct hash!
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

andyandy
  • Members
  • 11 posts
  • Last active: May 03 2015 11:14 AM
  • Joined: 19 Feb 2014

Yeah I have updated the AHK, but the issue is that your script can't generate it dynamically.



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
✓  Best Answer
it does... ill run this atm
Its the same script. Just the MD5 function is another one.

MD5.ahk (Source)
MD5.exe (Compiled)


I'll hope you DL AutoHotkey from here ==> AutoHotkey Downloads
v1.1.16.05 - September 28, 2014
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you