我正在按照以下步骤调用PSD2端点,对消息进行base64编码,然后获取SHA256以从中获取哈希值并获取哈希值的base64。我使用示例的相同值来检查我是否做对了。他们还为a website提供了一个js库来检查结果。
输入:
ewogICJpbnN0cnVjdGVkQW1vdW50IiA6IHsKICAgICJjdXJyZW5jeSIgOiAiRVVSIiwKICAgICJhbW91bnQiIDogIjE2LjAwIgogIH0sCiAgImRlYnRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNTE0MDAwMDAwMTA1MDAwMDAwMDAwMSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvck5hbWUiIDogIkNyZWQuIE5hbWUiLAogICJjcmVkaXRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNjYyMTAwMDQxODQwMTIzNDU2Nzg5MSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvckFkZHJlc3MiIDogewogICAgInN0cmVldCIgOiAiRWplbXBsbyBkZSBjYWxsZSIsCiAgICAiYnVpbGRpbmdOdW1iZXIiIDogIjE1IiwKICAgICJjaXR5IiA6ICJDb3Jkb2JhIiwKICAgICJwb3N0YWxDb2RlIiA6ICIxNDEwMCIsCiAgICAiY291bnRyeSIgOiAiRVMiCiAgfSwKICAicmVtaXR0YW5jZUluZm9ybWF0aW9uVW5zdHJ1Y3R1cmVkIiA6ICJQYWdvIiwKICAiY2hhcmdlQmVhcmVyIiA6ICJDUkVEIgp9
预期输出:
pfHPQFso5E7SlQfg9kSVhZuod4k9KnFFEtFs472L5WI=
我在做什么:
import base64
import hashlib
# get_input returns the input base64 in bytes
result = base64.b64encode(hashlib.sha256(get_input()).digest())
在这种情况下,结果是:
b'JRtx3taNOfx00oj2xuyoAxocxfJnL/wEXLYf9+t9jCk='
而不是预期的结果。
此结果与JS页面将输入类型从base64更改为text的结果相同,因此我认为输入是正确的。但是使用hashlib时,没有输入类型选项。所以我的问题是:我必须怎么做才能在python中用该输入获得预期的输出?
答案 0 :(得分:1)
网站正在解码来自base64的输入字符串,对其进行哈希处理,然后将哈希编码为base64。
>>> s = 'ewogICJpbnN0cnVjdGVkQW1vdW50IiA6IHsKICAgICJjdXJyZW5jeSIgOiAiRVVSIiwKICAgICJhbW91bnQiIDogIjE2LjAwIgogIH0sCiAgImRlYnRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNTE0MDAwMDAwMTA1MDAwMDAwMDAwMSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvck5hbWUiIDogIkNyZWQuIE5hbWUiLAogICJjcmVkaXRvckFjY291bnQiIDogewogICAgImliYW4iIDogIkVTNjYyMTAwMDQxODQwMTIzNDU2Nzg5MSIsCiAgICAiY3VycmVuY3kiIDogIkVVUiIKICB9LAogICJjcmVkaXRvckFkZHJlc3MiIDogewogICAgInN0cmVldCIgOiAiRWplbXBsbyBkZSBjYWxsZSIsCiAgICAiYnVpbGRpbmdOdW1iZXIiIDogIjE1IiwKICAgICJjaXR5IiA6ICJDb3Jkb2JhIiwKICAgICJwb3N0YWxDb2RlIiA6ICIxNDEwMCIsCiAgICAiY291bnRyeSIgOiAiRVMiCiAgfSwKICAicmVtaXR0YW5jZUluZm9ybWF0aW9uVW5zdHJ1Y3R1cmVkIiA6ICJQYWdvIiwKICAiY2hhcmdlQmVhcmVyIiA6ICJDUkVEIgp9'
>>> decoded = base64.b64decode(s)
>>> hash_ = hashlib.sha256(decoded)
>>> r = base64.b64encode(hash_.digest())
>>> r.decode()
'pfHPQFso5E7SlQfg9kSVhZuod4k9KnFFEtFs472L5WI='
答案 1 :(得分:0)
尝试解码结果:
result = base64.b64encode(hashlib.sha256("hi".encode()).digest())
print(result)
print(result.decode('utf-8'))
输出:
b'j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ='
j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=