Jump to content

PHP to AutoIt.... Quick question regarding hash_hmac & base64_encode


Recommended Posts

Hello Everyone!

I have a PHP script that I am trying to convert into AutoIt and have a silly question. Can anyone help me out in converting this one line into AutoIt?

The following is the PHP code that works

// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, $SECRET_ACCESS_KEY, TRUE);

// Base64 encode the signature and make it URL safe
$signature = urlencode(base64_encode($signature));

I'm trying to figure out how to do this in AutoIt. I found two functions here: https://www.autoitscript.com/forum/topic/165093-funnot-with-hashing-sha256/?do=findComment&comment=1205352 But I cant seem to figure out how to get this to work or get my results to match what the above php code will put out...

If there is anyone out there that can show me how to do this I would be forever in your debt!


Thanks for the time in reviewing my problem.

Edited by Keltset


Link to comment
Share on other sites

There is a base64 UDF, referenced here: https://www.autoitscript.com/forum/topic/107501-simple-base64-encoderdecoder/

There is a crypt UDF in the standard help file (used in the example you cited).

Can you post what you have tried so far and be more specific about what is not working?

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

I made a mod of a function from this thread (because I don't like "call" funtion



#include <Crypt.au3>

Global Const $CALG_SHA_256 = 0x0000800c

ConsoleWrite(hmac_sha256("message", "key") & @CRLF)

Func hmac_sha256($message, $key)
    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 = _Crypt_HashData($key, $CALG_SHA_256)
    For $i = 1 To BinaryLen($key)
        $a_ipad[$i - 1] = Number(BinaryMid($key, $i, 1))
        $a_opad[$i - 1] = Number(BinaryMid($key, $i, 1))
    For $i = 0 To $blocksize - 1
        $a_opad[$i] = BitXOR($a_opad[$i], $oconst)
        $a_ipad[$i] = BitXOR($a_ipad[$i], $iconst)
    For $i = 0 To $blocksize - 1
        $ipad &= Binary('0x' & Hex($a_ipad[$i], 2))
        $opad &= Binary('0x' & Hex($a_opad[$i], 2))
    Return StringMid(_Crypt_HashData($opad & _Crypt_HashData($ipad & Binary($message), $CALG_SHA_256), $CALG_SHA_256), 3)
EndFunc   ;==>hmac_sha256


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...