I'm trying to convert python code that encode some data into apex and having some issue when the python uses function bytes

In python its

header = sha3_256(f"{bytes(str('someinput'), 'utf-8')}".encode("utf-8")).hexdigest()

And the apex translation is

String header = EncodingUtil.convertToHex(Crypto.generateDigest('SHA3-256', Blob.valueOf('someinput')));

but the output is not the same mainly because the function "bytes" in python which I don't know if and what is the equivalent in SF.

Any idea?

1 Answer 1


This answer was a try but does not solve the question , please read notes below.

I would say that you don't apply the functions in the same order :

  • In python you apply sha() to the bytes array for the string
  • In Apex you transform to bytes the result of sha function()

It can't be the same.

I would have translated python to :

String header = (

Note : it returns "not a valid utf8 string"

  • I don't know python but when I try to run your code I get a invalid syntax message Apr 30, 2021 at 7:44
  • Question is how I can do the same in Apex? Without the bytes() function in python it does give me the same result, but I don't think there is equivalent function in apex.
    – Liron C
    Apr 30, 2021 at 7:49
  • Can you please confirm if your python code works, or correct it ? when I run it in tutorialspoint.com/execute_python_online.php I have an error and I'm not able to correct it myself Apr 30, 2021 at 7:58
  • It works. Seems need to use python 3+, while the tool you use is older. programiz.com/python-programming/online-compiler
    – Liron C
    Apr 30, 2021 at 8:02
  • from hashlib import sha3_256 header = sha3_256(f"{bytes(str('someinput'), 'utf-8')}".encode("utf-8")).hexdigest() print(header)
    – Liron C
    Apr 30, 2021 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .