[SOLVED] Convert au3 to ahk

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

[SOLVED] Convert au3 to ahk

24 Jan 2014, 04:59

Need help to convert au3 to ahk

Code: Select all

#include <Crypt.au3>

Func _HASH($message, $algo)
    Return _Crypt_HashData($message, Eval('CALG_' & $algo))
EndFunc

Func hmac($key, $message, $algo="md5")
    Local $blocksize = 64
    Local $a_opad[$blocksize], $a_ipad[$blocksize]
    Local Const $oconst = 0x5C, $iconst = 0x36
    Local $opad = Binary(''), $ipad = Binary('')
    $key = Binary($key)
    If BinaryLen($key) > $blocksize Then $key = _HASH($key, $algo)
    For $i = 1 To BinaryLen($key)
        $a_ipad[$i-1] = Number(BinaryMid($key, $i, 1))
        $a_opad[$i-1] = Number(BinaryMid($key, $i, 1))
    Next
    For $i = 0 To $blocksize - 1
        $a_opad[$i] = BitXOR($a_opad[$i], $oconst)
        $a_ipad[$i] = BitXOR($a_ipad[$i], $iconst)
    Next
    For $i = 0 To $blocksize - 1
        $ipad &= Binary('0x' & Hex($a_ipad[$i],2))
        $opad &= Binary('0x' & Hex($a_opad[$i],2))
    Next
    ;Return Call($algo, $opad & Call($algo, $ipad & Binary($message)))
	Local $ret = _HASH($ipad & Binary($message), $algo)
    $ret = _HASH($opad & $ret, $algo)
    Return $ret
EndFunc

MsgBox(0, "HMAC - MD2",    hmac("key", "The quick brown fox jumped over the lazy dog", "MD2"    ))
MsgBox(0, "HMAC - MD4",    hmac("key", "The quick brown fox jumped over the lazy dog", "MD4"    ))
MsgBox(0, "HMAC - MD5",    hmac("key", "The quick brown fox jumped over the lazy dog", "MD5"    ))
MsgBox(0, "HMAC - SHA1",   hmac("key", "The quick brown fox jumped over the lazy dog", "SHA1"   ))
MsgBox(0, "HMAC - SHA256", hmac("key", "The quick brown fox jumped over the lazy dog", "SHA_256"))
MsgBox(0, "HMAC - SHA384", hmac("key", "The quick brown fox jumped over the lazy dog", "SHA_384"))
MsgBox(0, "HMAC - SHA512", hmac("key", "The quick brown fox jumped over the lazy dog", "SHA_512"))
MD2, MD4, MD5, SHA-1 & SHA-256 = blocksize 64
SHA-384 & SHA-512 = blocksize 128

Just need this hmac function (no hash function)

Code: Select all

Text    The quick brown fox jumped over the lazy dog
Key     key

MD2     05313c6f987a9cf3feebc672b6717aca
MD4     e04a858c5c515a4c451830a5cde555d8
MD5     253ea60a3f817facebabf581c7c4e4e2
SHA1    5e91db3ec02e06cc0c81aa796814804bbf769490
SHA256  5a37f0d8ae01dd96bdb5fab32cdf9383b12f37d6526adffd52c8adac0d8ca866
SHA384  511e4f49e10a043e788cb2f478e9e2e5fc7af074b057604f8589ce382c28885f9fd8df5db3794cab355f1098307a4980
SHA512  4b4b2482b4351cfc98ed927d1e5b08cce547549f5c106f028391027228079725094ec5b01fcaa88137a5834f2110d4ede3afa1aee82844f9dc4bd3851ba1c65b
Last edited by jNizM on 10 Feb 2014, 02:19, edited 4 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Convert au3 to ahk + expand functions

25 Jan 2014, 13:28

jNizM iirc your other amazing lib has SHA-384 & SHA-512 and HMAC no?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk + expand functions

26 Jan 2014, 06:39

wat?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk + expand functions

26 Jan 2014, 09:29

I know, but..

1. I dont use Deo's lib, I use the function from Bentschi --> Hash
2. The HMAC function in Deo's lib generates a different hash value (Not the same hash like the real HMAC function generates)
3. This AutoIt script generates the real HMAC values from MD2, MD4, MD5, SHA-1, SHA-256

HMAC on wiki
HashCalc tool (with HMAC)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk + expand functions

27 Jan 2014, 05:34

Ok I found out that the blocksize need to be 128 for sha384 & sha512

Can someone help me to translate the pure hmac function from au3 to ahk?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Convert au3 to ahk

29 Jan 2014, 16:07

not sure if you saw this
TLM wrote:
jNizM wrote:Would be nice to short ... a .. line like this..

Code: Select all

if (algo = "md2" || "md4" || "md5" || "sha1" || "sha256")
what about shorthand alternates ??
if (algo ~= "(md2|md4|md5|sha1|sha256)")


'classesk' alternate, alternates
if (algo ~= "md(2|4|5)|sha(1|256)") ;)

you may need anchors jNizM :)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk

30 Jan 2014, 01:50

yes, and thank you for this.. but its just a short help ;)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
just me
Posts: 9528
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert au3 to ahk

31 Jan 2014, 11:19

This seems to work for MD5, but the length check for Key is not implemented:

Code: Select all

#NoEnv
SetBatchLines, -1
MsgBox, 0, HMAC, % HMAC("key", "The quick brown fox jumps over the lazy dog")
MsgBox, 0, HMAC, % HMAC("", "")
ExitApp
; ===================================================================================
HMAC(Key, Message, Algo := "MD5") {
   Static BlockSize := 64
   Static IC := 0x36                               ; $iconst
   Static OC := 0x5C                               ; $oconst
   Static AlgIDs := {MD5: 0x8003}
   MsgLen := StrPut(Message, "UTF-8") - 1
   KeyLen := StrPut(Key, "UTF-8") - 1
   If (KeyLen) {
      VarSetCapacity(K, KeyLen, 0)
      StrPut(Key, &K, KeyLen, "UTF-8")
   }
   ; First part ---------------------------------------------------------------------
   VarSetCapacity(IP, BlockSize + MsgLen, IC)      ; $ipad
   I := 0
   While (I < KeyLen) {
      NumPut(NumGet(IP, I, "UChar") ^ NumGet(K, I, "UChar"), IP, I, "UChar")
      I++
   }
   If (MsgLen)
      StrPut(Message, &IP + BlockSize, MsgLen, "UTF-8")
   CalcAddrHash(&IP, BlockSize + MsgLen, AlgIDs[Algo], Hash1, Hash1Len)
   ; Second part --------------------------------------------------------------------
   VarSetCapacity(OP, BlockSize + Hash1Len, OC)    ; $opad
   I := 0
   While (I < KeyLen) {
      NumPut(NumGet(OP, I, "UChar") ^ NumGet(K, I, "UChar"), OP, I, "UChar")
      I++
   }
   Addr := &OP + BlockSize
   I := 0
   While (I < Hash1Len) {
      NumPut(NumGet(Hash1, I, "UChar"), Addr + I, 0, "UChar")
      I++
   }
   Return CalcAddrHash(&OP, BlockSize + Hash1Len, AlgIDs[Algo])
}
; ===================================================================================
; AHK Version ...: AHK_L 1.1.13.01 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: Checksum: MD5
;                  Calc MD5-Hash from String / File / Address
;                  http://en.wikipedia.org/wiki/MD5
; Version .......: 2014.01.06-1747
; Author ........: Bentschi
; Modified ......: jNizM
; ===================================================================================
; CalcAddrHash ======================================================================
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()
    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
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk

02 Feb 2014, 06:41

Thank you just me.. I will test it soon..

In au3 it looks more easier than in ahk -.-
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Convert au3 to ahk

03 Feb 2014, 04:52

just me, excellent work as always.
jNizM wrote:yes, and thank you for this.. but its just a short help ;)
Glad that I could be of some assistance ;)

jNizM, I think since the last encounter with crypt in AHk I've regressed back to
lol

I'm glad your still working on this, it really is a must have function in AHk.
If anything else comes to mind I will def. post : )
just me
Posts: 9528
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert au3 to ahk

04 Feb 2014, 01:14

THX, TLM.

Here's a 'completed' version seeming to work still (quickly tested on quickhash.com/hmac and http://www.freeformatter.com/hmac-generator.html):

Code: Select all

#NoEnv
SetBatchLines, -1
Algorithms := ["MD2", "MD4", "MD5", "SHA", "SHA256", "SHA384", "SHA512"]
Key := "key"
Msg := "The quick brown fox jumps over the lazy dog"
Out := "Text`t" . Msg . "`nKey`t" . Key . "`n"
Rows := 2 + Algorithms.MaxIndex() + 1
Gui, Margin, 10, 10
Gui, Font, , Courier New
Gui, Add, ListView, w980 r%Rows% -Hdr, 1|2
LV_Add("", "Text", Msg)
LV_Add("", "Key", Key)
For Each, Algo In Algorithms
   LV_Add("", Algo, HMAC(Key, Msg, Algo))
LV_ModifyCol()
Gui, Show, , Results
Return
GuiCLose:
GuiEscape:
ExitApp

; ======================================================================================================================
; Function:
;     Calculates a hash-based message authentication code (HMAC).
;     -> en.wikipedia.org/wiki/Hash-based_message_authentication_code)
; Parameters:
;     Key      -  Key as string.
;     Message  -  Message as string.
;     Algo     -  Hash algorithm to use (one of the string keys defined in Algorithms).
; Return values:
;     Returns the hexadicimal HMAC value on success, otherwise an empty string ("").
; ======================================================================================================================
HMAC(Key, Message, Algo := "MD5") {
   Static Algorithms := {MD2: {ID: 0x8001, Size: 64}
                       , MD4: {ID: 0x8002, Size: 64}
                       , MD5: {ID: 0x8003, Size: 64}
                       , SHA: {ID: 0x8004, Size: 64}
                       , SHA256: {ID: 0x800C, Size: 64}
                       , SHA384: {ID: 0x800D, Size: 128}
                       , SHA512: {ID: 0x800E, Size: 128}}
   Static IC := 0x36 ; $iconst
   Static OC := 0x5C ; $oconst
   ; Init --------------------------------------------------------------------------------------------------------------
   If !Algorithms.HasKey(Algo)
      Return ""
   Hash := ""
   HashLen := 0
   AlgID := Algorithms[Algo].ID
   BlockSize := Algorithms[Algo].Size
   MsgLen := StrPut(Message, "UTF-8") - 1
   KeyLen := StrPut(Key, "UTF-8") - 1
   VarSetCapacity(K, KeyLen + 1, 0)
   StrPut(Key, &K, KeyLen, "UTF-8")
   If (KeyLen > BlockSize)
      HMAC_Hash(&K, KeyLen, AlgID, KeyHash, KeyHashLen)
   ; Inner hash --------------------------------------------------------------------------------------------------------
   VarSetCapacity(IP, BlockSize + MsgLen, IC) ; $ipad
   Addr := KeyLen > BlockSize ? &KeyHash : &K
   Length := KeyLen > BlockSize ? KeyHashLen : KeyLen
   I := 0
   While (I < Length) {
      NumPut(NumGet(Addr + 0, I, "UChar") ^ IC, IP, I, "UChar")
      I++
   }
   If (MsgLen)
      StrPut(Message, &IP + BlockSize, MsgLen, "UTF-8")
   HMAC_Hash(&IP, BlockSize + MsgLen, AlgID, InnerHash, InnerHashLen)
   ; Outer hash --------------------------------------------------------------------------------------------------------
   VarSetCapacity(OP, BlockSize + InnerHashLen, OC) ; $opad
   Addr := KeyLen > BlockSize ? &KeyHash : &K
   Length := KeyLen > BlockSize ? KeyHashLen : KeyLen
   I := 0
   While (I < Length) {
      NumPut(NumGet(Addr + 0, I, "UChar") ^ OC, OP, I, "UChar")
      I++
   }
   Addr := &OP + BlockSize
   I := 0
   While (I < InnerHashLen) {
      NumPut(NumGet(InnerHash, I, "UChar"), Addr + I, 0, "UChar")
      I++
   }
   Return HMAC_Hash(&OP, BlockSize + InnerHashLen, AlgID)
}
; ======================================================================================================================
; Originally released by jNizM at 
; http://www.autohotkey.com/board/topic/89237-bentschis-funktion-hash-von-strings-erweitert-um-sha2-klassen/
; Function:
;     Calculates a hash.
; Parameters:
;     DataAddr    -  Address of the data to hash.
;     DataLength  -  Length of the data to hash.
;     AlgID       -  ID of the hash algorithm to use (see HMAC() -> Algorithms).
;     -------------  Optional:
;     HashValue   -  Variable to return the binary hash value.
;     HashLen     -  Variable to return the length of the binary hash.
; Return Values:
;     Returns the hexadecimal hash value on success, otherwise an empty string (""). 
; ======================================================================================================================
HMAC_Hash(DataAddr, DataLength, AlgID, ByRef HashValue := "", ByRef HashLength := 0) {
   Static X := {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9
              , 10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F"}
   Result := ""
   If DllCall("advapi32.dll\CryptAcquireContext"
            , "PtrP", Prov, "Ptr", 0, "Ptr", 0, "UInt", 24, "UInt", 0xF0000000) {
      If DllCall("advapi32.dll\CryptCreateHash"
               , "Ptr", Prov, "UInt", AlgID, "UInt", 0, "UInt", 0, "PtrP", Hash) {
         If DllCall("advapi32.dll\CryptHashData"
                  , "Ptr", Hash, "Ptr", DataAddr, "UInt", DataLength, "UInt", 0) {
            If DllCall("advapi32.dll\CryptGetHashParam"
                     , "Ptr", Hash, "UInt", 2, "Ptr", 0, "UIntP", HashLength, "UInt", 0) {
               VarSetCapacity(HashValue, HashLength, 0)
               If DllCall("advapi32.dll\CryptGetHashParam"
                        , "Ptr", Hash, "UInt", 2, "Ptr", &HashValue, "UIntP", HashLength, "UInt", 0) {
                  Loop, % HashLength
                     Result .= X[(V := NumGet(HashValue, A_Index - 1, "UChar")) >> 4] . X[V & 0x0F]
               }
            }
         }
         DllCall("advapi32.dll\CryptDestroyHash", "Ptr", Hash)
      }
      DllCall("advapi32.dll\CryPtreleaseContext", "Ptr", Prov, "UInt", 0)
   }
   Return Result
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Convert au3 to ahk

04 Feb 2014, 08:07

thanks just me for your amazing help

Hash from string or file, HMAC, Salt
Hash Calculator

and thanks to TLM for your idea
if (algo ~= "md(2|4|5)|sha(1|256)")
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu, Google [Bot], Ragnar and 110 guests