web3使用什么hash加密算法? ethereumjs-util Sha3 returns 不同的结果

What hash encryption algorithm does web3 use? ethereumjs-util Sha3 returns different result

我正在尝试通过 web3 的 signPersonalMessage 函数正确签署身份验证方法。但是,正确验证的 eth-sig-util 包不会生成具有与 SHA3-256 和 Keccak-256 相同数据的相同散列。

例如,如果我尝试散列: 0x664f5f32784d335a69596e4f416c636373496859377054744f704c6e54666a314c70314a4f364d565f4638

我在 Keccak256 上的结果: 5dc2751b78fdb82a0986ad2a8e6cc774458d7a096e8eafd3ab6937146b7d2eec

我的 SHA3-256 结果: 6fa399a29899ebab112d562526b9b735f0cf7ce4bdc5a96edfcc48572b8d3e7b

我使用库的 Sha3 函数得到的结果: 40de9eb1ead6a6299af30cdf51eca39d44be214ce7431c93ff546cf5fb1ef0fb

这是我唯一能想到的问题,我之前在尝试使用 Keccak 进行签名和身份验证时也经常遇到 'Incorrect v length' 或 'invalid signature length' 错误。

我猜你使用的是 web3.js 0.20.x 并且未能传递 encoding 参数。这会将输入视为字符串,这可能不是您想要的:

web3.sha3('0x664f5f32784d335a69596e4f416c636373496859377054744f704c6e54666a314c70314a4f364d565f4638', { encoding: 'hex' })
"0x40de9eb1ead6a6299af30cdf51eca39d44be214ce7431c93ff546cf5fb1ef0fb"

web3 有 2 个使用的方法:

web3.utils.sha3 
web3.utils.soliditySha3

尽管名称中包含“Sha3”,但实际上它们实现了 keccak256。不同之处在于数据的打包,soliditySha3 使用紧密打包。

tightly packed arguments