How to get the same hash in Python3 and Mac / Linux terminal?

How can I get the same sha256 hash file in terminal (Mac / Linux) and Python?

Tried various versions of the examples below and performed a search on StackOverflow.

Terminal:

echo 'test text' | shasum -a 256

c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00

Python3:

import hashlib
hashlib.sha256(str("test text").encode('utf-8')).hexdigest()

'0f46738ebed370c5c52ee0ad96dec8f459fb901c2ca4e285211eddf903bf1598'

Update: Unlike Why is the MD5 hash created by Python different from the one created using echo and md5sum in the shell? , because in Python3 you need to explicitly code, and I need a solution in Python, and not just in the terminal. Duplicate will not work with files:

example.txt:

test text

Terminal:

shasum -a 256 example.txt

c2a4f4903509957d138e216a6d2c0d7867235c61088c02ca5cf38f2332407b00

+4
source share
1 answer

echo , , .

echo -n 'test text' | shasum -a 256

( , POLA), python

hashlib.sha256("{}\n".format("test text").encode('utf-8')).hexdigest()
+7

Source: https://habr.com/ru/post/1696200/


All Articles