简体   繁体   中英

Unable to obtain the same sha256 hash in python and javascript

I am following the steps to call a PSD2 endpoint, base64 code the message, then get SHA256 to obtain the Hash from it and get the base64 of the hash. I am using the same values of the examples to check if I am doing it right. They also provide a website with a js library to check the result.

https://imgur.com/vsaTbvX

Input:

ewogICJpbnN0cnVjdGVkQW1vdW50IiA6IHsKICAgICJjdXJyZW5jeSIgOiAiRVVSIiwKICAgICJhbW91bnQiIDogIjE2LjAwIgogIH0sCiAgImRlYnRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNTE0MDAwMDAwMTA1MDAwMDAwMDAwMSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvck5hbWUiIDogIkNyZWQuIE5hbWUiLAogICJjcmVkaXRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNjYyMTAwMDQxODQwMTIzNDU2Nzg5MSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvckFkZHJlc3MiIDogewogICAgInN0cmVldCIgOiAiRWplbXBsbyBkZSBjYWxsZSIsCiAgICAiYnVpbGRpbmdOdW1iZXIiIDogIjE1IiwKICAgICJjaXR5IiA6ICJDb3Jkb2JhIiwKICAgICJwb3N0YWxDb2RlIiA6ICIxNDEwMCIsCiAgICAiY291bnRyeSIgOiAiRVMiCiAgfSwKICAicmVtaXR0YW5jZUluZm9ybWF0aW9uVW5zdHJ1Y3R1cmVkIiA6ICJQYWdvIiwKICAiY2hhcmdlQmVhcmVyIiA6ICJDUkVEIgp9

Expected Output:

pfHPQFso5E7SlQfg9kSVhZuod4k9KnFFEtFs472L5WI=

What I am doing:

import base64
import hashlib


# get_input returns the input base64 in bytes
result = base64.b64encode(hashlib.sha256(get_input()).digest())

In that case, the result is:

b'JRtx3taNOfx00oj2xuyoAxocxfJnL/wEXLYf9+t9jCk='

Instead of the expected result.

, so I assume the input is correct.,因此我假设输入是正确的。 But with hashlib there are not input type options. So my question is: What I have to do to get the expected output with that input in python?

The website is decoding the input string from base64, hashing it, and then encoding the hash as base64.

>>> s = 'ewogICJpbnN0cnVjdGVkQW1vdW50IiA6IHsKICAgICJjdXJyZW5jeSIgOiAiRVVSIiwKICAgICJhbW91bnQiIDogIjE2LjAwIgogIH0sCiAgImRlYnRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNTE0MDAwMDAwMTA1MDAwMDAwMDAwMSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvck5hbWUiIDogIkNyZWQuIE5hbWUiLAogICJjcmVkaXRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNjYyMTAwMDQxODQwMTIzNDU2Nzg5MSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvckFkZHJlc3MiIDogewogICAgInN0cmVldCIgOiAiRWplbXBsbyBkZSBjYWxsZSIsCiAgICAiYnVpbGRpbmdOdW1iZXIiIDogIjE1IiwKICAgICJjaXR5IiA6ICJDb3Jkb2JhIiwKICAgICJwb3N0YWxDb2RlIiA6ICIxNDEwMCIsCiAgICAiY291bnRyeSIgOiAiRVMiCiAgfSwKICAicmVtaXR0YW5jZUluZm9ybWF0aW9uVW5zdHJ1Y3R1cmVkIiA6ICJQYWdvIiwKICAiY2hhcmdlQmVhcmVyIiA6ICJDUkVEIgp9'
>>> decoded = base64.b64decode(s)
>>> hash_ = hashlib.sha256(decoded)
>>> r = base64.b64encode(hash_.digest())
>>> r.decode()
'pfHPQFso5E7SlQfg9kSVhZuod4k9KnFFEtFs472L5WI='

Try to decode the result:

result = base64.b64encode(hashlib.sha256("hi".encode()).digest())
print(result)
print(result.decode('utf-8'))

Output:

b'j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ='
j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=

source: https://docs.python.org/3/howto/unicode.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM