Keltset

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

3 posts in this topic

#1 ·  Posted (edited)

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

-K

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

https://www.autoitscript.com/forum/topic/165093-funnot-with-hashing-sha256/?do=findComment&comment=1205352

 

#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))
    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 StringMid(_Crypt_HashData($opad & _Crypt_HashData($ipad & Binary($message), $CALG_SHA_256), $CALG_SHA_256), 3)
EndFunc   ;==>hmac_sha256

Saludos

Share this post


Link to post
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