How can I generate a HMAC SHA-1 hash

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

How can I generate a HMAC SHA-1 hash

31 Oct 2022, 10:09

I have difficulties to generate a hash HMAC SHA-1 with our scripts e.g.

Github ....: https://github.com/jNizM/AHK_CNG
Forum .....: viewtopic.php?f=6&t=23413

On the page https://www.liavaag.org/English/SHA-Generator/HMAC/
I succeed with the following values:

Input : 0000000002efd5b8
Input Type: Hex

Key : 3DC6CAA4824A6D288767B2331E20B43166CB85D9
Key Type : Hex

SHA variant : SHA-1
Output type: Hex

Result : 7f43d4dbec5836a89eb12470b1f47058e0a982d7

Maybe someone can explain me how to do it with AHK.
teadrinker
Posts: 4362
Joined: 29 Mar 2015, 09:41
Contact:

Re: How can I generate a HMAC SHA-1 hash

31 Oct 2022, 12:06

Code: Select all

BCRYPT_SHA1_ALGORITHM := "SHA1"

hex := "0000000002efd5b8"
key := "3DC6CAA4824A6D288767B2331E20B43166CB85D9"
; result: 7f43d4dbec5836a89eb12470b1f47058e0a982d7

dataLength := CryptStringToBinary(hex, binData, "CRYPT_STRING_HEXRAW")
keyLength  := CryptStringToBinary(key, binKey , "CRYPT_STRING_HEXRAW")
hashLength := CreateHash(&binData, dataLength, binHash, &binKey, keyLength, BCRYPT_SHA1_ALGORITHM)
MsgBox, % CryptBinaryToString(&binHash, hashLength, "CRYPT_STRING_HEXRAW")

CreateHash(pData, size, ByRef hashData, pSecretKey := 0, keySize := 0, AlgId := "SHA256") {
   ; CNG Algorithm Identifiers
   ; https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-identifiers
   static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x8  ; HMAC
   
   DllCall("Bcrypt\BCryptOpenAlgorithmProvider", "PtrP", hAlgorithm, "WStr", AlgId, "Ptr", 0, "UInt", keySize ? BCRYPT_ALG_HANDLE_HMAC_FLAG : 0)
   DllCall("Bcrypt\BCryptCreateHash", "Ptr", hAlgorithm, "PtrP", hHash, "Ptr", 0, "UInt", 0, "Ptr", pSecretKey, "UInt", keySize, "UInt", 0)
   DllCall("Bcrypt\BCryptHashData", "Ptr", hHash, "Ptr", pData, "UInt", size, "UInt", 0)
   DllCall("Bcrypt\BCryptGetProperty", "Ptr", hAlgorithm, "WStr", "HashDigestLength", "UIntP", hashLen, "UInt", 4, "UIntP", cbResult, "UInt", 0)
   VarSetCapacity(hashData, hashLen, 0)
   DllCall("Bcrypt\BCryptFinishHash", "Ptr", hHash, "Ptr", &hashData, "UInt", hashLen, "UInt", 0)
   DllCall("Bcrypt\BCryptDestroyHash", "Ptr", hHash)
   DllCall("Bcrypt\BCryptCloseAlgorithmProvider", "Ptr", hAlgorithm, "UInt", 0)
   Return hashLen
}

CryptBinaryToString(pData, size, formatName := "CRYPT_STRING_BASE64", NOCRLF := true)
{
   static formats := { CRYPT_STRING_BASE64: 0x1
                     , CRYPT_STRING_HEX:    0x4
                     , CRYPT_STRING_HEXRAW: 0xC }
        , CRYPT_STRING_NOCRLF := 0x40000000
   fmt := formats[formatName] | (NOCRLF ? CRYPT_STRING_NOCRLF : 0)
   if !DllCall("Crypt32\CryptBinaryToString", "Ptr", pData, "UInt", size, "UInt", fmt, "Ptr", 0, "UIntP", chars)
      throw "CryptBinaryToString failed. LastError: " . A_LastError
   VarSetCapacity(outData, chars << !!A_IsUnicode)
   DllCall("Crypt32\CryptBinaryToString", "Ptr", pData, "UInt", size, "UInt", fmt, "Str", outData, "UIntP", chars)
   Return outData
}

CryptStringToBinary(string, ByRef outData, formatName := "CRYPT_STRING_BASE64")
{
   static formats := { CRYPT_STRING_BASE64: 0x1
                     , CRYPT_STRING_HEX:    0x4
                     , CRYPT_STRING_HEXRAW: 0xC }
   fmt := formats[formatName]
   chars := StrLen(string)
   if !DllCall("Crypt32\CryptStringToBinary", "Str", string, "UInt", chars, "UInt", fmt
                                            , "Ptr", 0, "UIntP", bytes, "Ptr", 0, "Ptr", 0)
      throw "CryptStringToBinary failed. LastError: " . A_LastError
   VarSetCapacity(outData, bytes)
   DllCall("Crypt32\CryptStringToBinary", "Str", string, "UInt", chars, "UInt", fmt
                                        , "Str", outData, "UIntP", bytes, "Ptr", 0, "Ptr", 0)
   Return bytes
}
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: How can I generate a HMAC SHA-1 hash

31 Oct 2022, 14:38

Wow!
Thank you very much. Although I don't think I will understand. Great. Super.
I want to use it to recreate the Google authenticator.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Chunjee, CoffeeChaton, erotoman and 102 guests