Dear All,
I am trying to generate SHA256 equivalent of a string using python code using the following code:
This code works perfectly fine inside a dofile. But when I try to use this file inside the following ado program, and pass the value of string from stata ado block to python block, this gives an error:
Can anyone please help me understand what is the error in this ado program and how to fix that?
Thank you.
Best regards,
Amit
I am trying to generate SHA256 equivalent of a string using python code using the following code:
Code:
local string = "AMIT" version 18.0 python: import hashlib from sfi import Macro # Get the string from Stata fixed_string = r""" `string' """ fixed_string = fixed_string.strip('"\' ') # Remove any extra quotes def generate_sha256(input_string): input_bytes = input_string.encode('utf-8') # Convert input to bytes hash_obj = hashlib.sha256() # Create SHA-256 hash object hash_obj.update(input_bytes) # Compute hash return hash_obj.hexdigest() # Return hexadecimal hash # Generate SHA-256 hash sha256_hash = generate_sha256(fixed_string) # Store result in Stata macro Macro.setLocal("sha256_hash", sha256_hash) print(f"Input String: {fixed_string}") print(f"SHA256: {sha256_hash}") end
Code:
program define genSHA2 version 18.0 syntax, STRing(str) di "`string'" end version 18.0 python: import hashlib from sfi import Macro # Get the string from Stata fixed_string = r""" `string' """ fixed_string = fixed_string.strip('"\' ') # Remove any extra quotes def generate_sha256(input_string): input_bytes = input_string.encode('utf-8') # Convert input to bytes hash_obj = hashlib.sha256() # Create SHA-256 hash object hash_obj.update(input_bytes) # Compute hash return hash_obj.hexdigest() # Return hexadecimal hash # Generate SHA-256 hash sha256_hash = generate_sha256(fixed_string) # Store result in Stata macro Macro.setLocal("sha256_hash", sha256_hash) print(f"Input String: {fixed_string}") print(f"SHA256: {sha256_hash}") end di "`sha256_hash'"
Thank you.
Best regards,
Amit
Comment