试图将"golang.org/x/crypto/sha3“库与solidity sha3()匹配,这给我带来了困难。它已经在这里中讨论过了,但是我不知怎么不能把它应用到go中。我如何处理solidity.sha3(uint256(1))==golang.sha3.Sum256(convertMyBigInt(myBigInt))类型的big.Int类型?
我将bigInt转换成十六进制字节数组,然后用零填充左边的站点,散列,然后输出。
//create a big int for test and set to 1
b1 := new(big.Int)
b1.SetInt64(1)
//create empty array with 32 bytes for padding
empty := make([]byte,32)
//turn the big int into a hex string (let me know if there is a more
//elegant way^^)
String := bytes.NewBufferString(b1.Text(16))
copy(empty[len(empty)-len(String.Bytes()):],String.Bytes())
//output of empty : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49]
res:= new(big.Int)
temp:=sha3.Sum256(empty)
res.SetBytes(temp[:])
fmt.Println("\nresult ",res.Text(16))输出: 26502030f0954243c70cb7fa68e0752ce2bf99aabfc0219d5184635d4c61dbd8
坚实给了我:
sha3(uint(1))
b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6有人能给我举个简单的例子吗?如何以优雅的方式将稳健的sha3与金刚sha3相匹配?我会非常感激的!
发布于 2018-06-22 09:35:53
下面是一个关于从sha3生成稳固的uint256散列的完整示例:
package main
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/crypto/sha3"
)
func main() {
h := sha3.NewKeccak256()
h.Write(abi.U256(big.NewInt(1)))
hash := h.Sum(nil)
fmt.Printf("%x", hash) // b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
}稳固性:
sha3(uint(1))
b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6我已经使库前进-坚实-3能够轻松地将实体类型转换为Go中的实体sha3散列。下面是一个例子:
package main
import (
"fmt"
"math/big"
solsha3 "github.com/miguelmota/go-solidity-sha3"
)
func main() {
hash := solsha3.SoliditySHA3(
solsha3.Uint256(big.NewInt(1)),
)
fmt.Printf("%x", hash) // b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
}一个更复杂的例子:
package main
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/miguelmota/go-solidity-sha3"
)
func main() {
hash := solsha3.SoliditySHA3(
solsha3.Address("0x12459c951127e0c374ff9105dda097662a027093"),
solsha3.Uint256(big.NewInt(100)),
solsha3.String("foo"),
solsha3.Bytes32("bar"),
solsha3.Bool(true),
)
fmt.Println(hex.EncodeToString(hash)) // 417a4c44724701ba79bb363151dff48909bc058a2c75a81e9cf5208ae4699369
}https://ethereum.stackexchange.com/questions/51566
复制相似问题