Global $HMAC_RAWFORM = 0, $HMAC_HEXFORM = 1, $HMAC_PYFORM = 2
Local $sSecret = "SECee08ec11058d84f58649214a037ebeb6d31f390adbe48336135927db595aa7f3"
Local $timestamp = "1692243595561"
Local $sMessage = $timestamp & @LF & $sSecret
Local $stringToSign = _HashHMAC("SHA256", $sMessage, $sSecret, $HMAC_RAWFORM)
CW("SHA256: " & String($stringToSign))
$stringToSign = _Base64Encode($stringToSign)
CW("Base64: " & $stringToSign)
Func CW($string)
ConsoleWrite($string & @CRLF)
EndFunc ;==>CW
Func _WinAPI_Base64Decode($sB64String)
Local $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0)
If @error Or Not $aCrypt[0] Then Return SetError(1, 0, "")
Local $tBuffer = DllStructCreate("byte[" & $aCrypt[5] & "]")
$aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "struct*", $tBuffer, "dword*", $aCrypt[5], "ptr", 0, "ptr", 0)
If @error Or Not $aCrypt[0] Then Return SetError(2, 0, "")
Return DllStructGetData($tBuffer, 1)
EndFunc ;==>_WinAPI_Base64Decode
Func _HashHMAC($sAlgorithm, $bData, $bKey, $iOutputForm = $HMAC_HEXFORM)
Local $oHashHMACErrorHandler = ObjEvent("AutoIt.Error", "_HashHMACErrorHandler")
Local $oHMAC = ObjCreate("System.Security.Cryptography.HMAC" & $sAlgorithm)
If @error Then SetError(1, 0, "")
$oHMAC.key = Binary($bKey)
Local $bHash = $oHMAC.ComputeHash_2(Binary($bData))
Local $sHex = StringLower(StringMid($bHash, 3))
Switch $iOutputForm
Case $HMAC_RAWFORM
Return $bHash
Case $HMAC_HEXFORM
Return $sHex
Case $HMAC_PYFORM
Local $c, $sPy
For $i = 1 To BinaryLen($bHash)
$c = BinaryMid($bHash, $i, 1)
If $c < 0x20 Or $c > 0x7E Then
$sPy &= "\x" & StringLower(Hex($c, 2))
Else
$sPy &= ($c = 0x5C ? "\" : Chr($c))
EndIf
Next
Return $sPy
Case Else
Return SetError(2)
EndSwitch
EndFunc ;==>_HashHMAC
Func _Base64Encode($input)
If Not IsBinary($input) Then $input = Binary($input)
Local $struct = DllStructCreate("byte[" & BinaryLen($input) & "]")
DllStructSetData($struct, 1, $input)
Local $strc = DllStructCreate("int")
Local $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
"ptr", DllStructGetPtr($struct), _
"int", DllStructGetSize($struct), _
"int", 1 + 0x40000000, _
"ptr", 0, _
"ptr", DllStructGetPtr($strc))
If @error Or Not $a_Call[0] Then
Return SetError(1, 0, "") ; error calculating the length of the buffer needed
EndIf
Local $a = DllStructCreate("char[" & DllStructGetData($strc, 1) & "]")
$a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _
"ptr", DllStructGetPtr($struct), _
"int", DllStructGetSize($struct), _
"int", 1 + 0x40000000, _
"ptr", DllStructGetPtr($a), _
"ptr", DllStructGetPtr($strc))
If @error Or Not $a_Call[0] Then
Return SetError(2, 0, "") ; error encoding
EndIf
Return DllStructGetData($a, 1)
EndFunc ;==>_Base64Encode