5582 Posted January 17, 2024 Posted January 17, 2024 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
Moderators Melba23 Posted January 17, 2024 Moderators Posted January 17, 2024 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Zedna Posted January 17, 2024 Posted January 17, 2024 (edited) 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 January 17, 2024 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search
argumentum Posted January 17, 2024 Posted January 17, 2024 15 minutes ago, Zedna said: don't know what is hmac and what AU3 alternative would be https://www.autoitscript.com/forum/topic/207244-how-to-get-binary-result-from-hmac-sha256-user-defined-function/#comment-1494275 Maybe ? Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
5582 Posted January 18, 2024 Author Posted January 18, 2024 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
5582 Posted January 19, 2024 Author Posted January 19, 2024 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. expandcollapse popupLocal $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 Zedna, Danyfirex and argumentum 3
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now