2

I am trying to replicate JS method:

JS code:


const toHexCopy = (buffer) => {
            return [...new Uint8Array (buffer)]
                .map (b => b.toString (16).padStart (2, "0"))
                .join ("");
};

ar = new Uint8Array([0, 1, 2]);
hash = await crypto.subtle.digest('SHA-256', ar)
console.log(toHexCopy(hash))

// returns 039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81

How do I replicate it in Python?

2
  • 1
    Do you want to hash a list or a sequence of bytes? Those are very different questions. Commented Apr 17, 2021 at 20:58
  • @Brian, basically I want to get same output as in JS. I have a list in Python, if needed, I can covert it to bytes. Commented Apr 17, 2021 at 21:00

1 Answer 1

3
from hashlib import sha256
print(sha256(bytes([1, 2, 3])).hexdigest())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.