Jump to content

Convert Python to Autoit3 and seek help.


Recommended Posts

Can the following Python code be converted to autoit3? Is there any expert who can help me? Thank you.

import hashlib
import base64
import hmac

def gen_sign(timestamp, secret):
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()
    sign = base64.b64encode(hmac_code).decode('utf-8')
    return sign

 

Link to comment
Share on other sites

  • Moderators

5582,

Welcome to the AutoIt forums.

I can imagine that many of the forum members are not familiar with Python coding and so cannot determine what that code is actually doing. Please explain what is happening and give a clue as to what you want to do.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

There are several UDFs for Base64 encoding and generating SHA

Search Examples section of this forum (in titles only) for Base64 and SHA

https://www.autoitscript.com/forum/search/?&q=base64&type=forums_topic&quick=1&nodes=9&search_and_or=and&search_in=titles&sortby=relevancy

https://www.autoitscript.com/forum/search/?&q=sha&type=forums_topic&quick=1&nodes=9&search_and_or=and&search_in=titles&sortby=relevancy

 

Declaring AU3 function doing the same as your Python script would be easy when you choose one of Base64 and SHA functions from (forum) Examples ...

 

EDIT: I just don't know what is hmac and what AU3 alternative would be for this.

Edited by Zedna
Link to comment
Share on other sites

Thanks. In this webpage, the HmacSHA256 algorithm is needed to calculate the signature. However, I have searched a lot of information online, and both the content value and the KEY value need to be entered. However, in this algorithm signature, it seems that only one content value was entered without seeing the key value. I am not sure what is going on, and I hope to get an answer. Meanwhile, I hope to implement it using autoit3 code

https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot

Link to comment
Share on other sites

The problem has been solved, thank you very much to this friend. 
Danyfirex https://www.autoitscript.com/forum/profile/71248-danyfirex/
At the same time, share the code, and if there are other friends who need it, they can refer to and learn from it.

Local $sSecret = "b8pW9XRUxIDdapjpLcjFte"
Local $iTimeStamp= 1705638000


ConsoleWrite("HMAC-SHA256: " & @TAB & @TAB & _HashHMAC($sSecret,$iTimeStamp) & @CRLF)

Func _HashHMAC($sSecret,$TimeStamp)
    Local $oHashHMACErrorHandler = ObjEvent("AutoIt.Error", "_HashHMACErrorHandler")
    Local $oHMAC = ObjCreate("System.Security.Cryptography.HMACSHA256")
    If @error Then SetError(1, 0, "")
    Local $sStrToSign =StringFormat("%s\n%s",$TimeStamp,$sSecret)
    $oHMAC.key = Binary($sStrToSign)
    Local $bHash = $oHMAC.ComputeHash_2(Binary(""))
    Return _Base64Encode($bHash)
EndFunc   ;==>_HashHMAC


Func _Base64Encode($input)

    $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, _
            "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, _
            "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

Func _HashHMACErrorHandler($oError)
    ;Dummy Error Handler
EndFunc   ;==>_HashHMACErrorHandler

 

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
 Share

  • Recently Browsing   0 members

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