python中的sha256(new Buffer(string,'hex'))

2024-09-26 18:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我用nodejs编写了这段代码。在

const sha256 = require('js-sha256').sha256;
var stringl = "8d12a197cb00d4747a1fe03395095ce2a5cc68198f3470a7388c05ee4e7af3d01d8c722b0ff5237400000000000000000000000000000000000000000000000000000000000000b40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000004c4b40000000000000000000000000000000000000000000000000000000000001e240"
console.log(sha256(new Buffer(stringl, 'hex')));

它回来了

^{pr2}$

我想把代码从nodejs转换成python(3.5)。在

我以为我可以用这个代码来做。在

import hashlib
string21 = "8d12a197cb00d4747a1fe03395095ce2a5cc68198f3470a7388c05ee4e7af3d01d8c722b0ff5237400000000000000000000000000000000000000000000000000000000000000b40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000004c4b40000000000000000000000000000000000000000000000000000000000001e240"
print(hashlib.sha256(str(int(string21,16)).encode('utf-8')).hexdigest())

但这又回来了

346c61765546d4ff12fdbea997f40bdb5216b8df2b5402a6b45893df723132f6

有什么想法吗?在


Tags: 代码lognewvarbufferjsnodejsrequire
1条回答
网友
1楼 · 发布于 2024-09-26 18:11:31

Buffer返回一个字节数组(range(0, 256)中的整数),因此您需要在Python中执行相同的操作:

print(hashlib.sha256(bytearray.fromhex(string21)).hexdigest())

bytearray.fromhex正是它所说的:从十六进制表示创建一个bytearray。在

编辑:这里不需要encode任何东西,因为bytearrays已经表示字节了。在

相关问题 更多 >

    热门问题