简体   繁体   English

无法在 python 和 javascript 中获得相同的 sha256 hash

[英]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 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.他们还提供了一个带有 js 库的网站来检查结果。

https://imgur.com/vsaTbvX https://imgur.com/vsaTbvX

Input:输入:

ewogICJpbnN0cnVjdGVkQW1vdW50IiA6IHsKICAgICJjdXJyZW5jeSIgOiAiRVVSIiwKICAgICJhbW91bnQiIDogIjE2LjAwIgogIH0sCiAgImRlYnRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNTE0MDAwMDAwMTA1MDAwMDAwMDAwMSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvck5hbWUiIDogIkNyZWQuIE5hbWUiLAogICJjcmVkaXRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNjYyMTAwMDQxODQwMTIzNDU2Nzg5MSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvckFkZHJlc3MiIDogewogICAgInN0cmVldCIgOiAiRWplbXBsbyBkZSBjYWxsZSIsCiAgICAiYnVpbGRpbmdOdW1iZXIiIDogIjE1IiwKICAgICJjaXR5IiA6ICJDb3Jkb2JhIiwKICAgICJwb3N0YWxDb2RlIiA6ICIxNDEwMCIsCiAgICAiY291bnRyeSIgOiAiRVMiCiAgfSwKICAicmVtaXR0YW5jZUluZm9ybWF0aW9uVW5zdHJ1Y3R1cmVkIiA6ICJQYWdvIiwKICAiY2hhcmdlQmVhcmVyIiA6ICJDUkVEIgp9

Expected Output:预期 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.而不是预期的结果。

This result is the same as the result in that JS page changing the input type from base64 to text , so I assume the input is correct.此结果与 JS 页面将输入类型从 base64 更改为 text 的结果相同,因此我假设输入是正确的。 But with hashlib there are not input type options.但是对于 hashlib,没有输入类型选项。 So my question is: What I have to do to get the expected output with that input in python?所以我的问题是:我必须做些什么来获得预期的 output 与 python 中的输入?

The website is decoding the input string from base64, hashing it, and then encoding the hash as base64.该网站正在解码来自 base64 的输入字符串,对其进行哈希处理,然后将 hash 编码为 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: Output:

b'j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ='
j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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