我使用以下代码生成 ECDSA key 对(privKey
和 pubKey
),对它们进行编码,然后将它们解码回来:https://stackoverflow.com/a/41315404/1901320 .
接下来,我使用 crypto.Keccak256()
为消息创建哈希(txnData.Payload()
的类型为 []byte
) > 并使用以太坊加密包 (github.com/ethereum/go-ethereum/crypto
) 中的 crypto.Sign()
对其进行签名。这会在 R || 中创建 65 位 ECDSA 签名S || V 格式。
hashData := crypto.Keccak256(txnData.Payload)
sig, _ := crypto.Sign(hashData, privKey)
pkey, _ := crypto.Ecrecover(hashData, sig) // This and pubKey do not match
当我尝试使用 crypto.Ecrecover()
从 hashData
和 ECDSA 签名取回公钥并将其与公钥 pubKey 进行比较时与用于创建签名的
,我发现公钥不匹配。这似乎不应该发生。知道我哪里出了问题吗?privKey
对应的
最佳答案
这是如何使用 go-ethereum 生成和验证签名的完整工作示例。
package main
import (
"bytes"
"crypto/ecdsa"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.PublicKey
publicKeyBytes := crypto.FromECDSAPub(&publicKey)
data := []byte("hello")
hash := crypto.Keccak256Hash(data)
fmt.Println(hash.Hex()) // 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
signature, err := crypto.Sign(hash.Bytes(), privateKey)
if err != nil {
log.Fatal(err)
}
fmt.Println(hexutil.Encode(signature)) // 0x789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301
sigPublicKey, err := crypto.Ecrecover(hash.Bytes(), signature)
if err != nil {
log.Fatal(err)
}
matches := bytes.Equal(sigPublicKey, publicKeyBytes)
fmt.Println(matches) // true
sigPublicKeyECDSA, err := crypto.SigToPub(hash.Bytes(), signature)
if err != nil {
log.Fatal(err)
}
sigPublicKeyBytes := crypto.FromECDSAPub(sigPublicKeyECDSA)
matches = bytes.Equal(sigPublicKeyBytes, publicKeyBytes)
fmt.Println(matches) // true
signatureNoRecoverID := signature[:len(signature)-1] // remove recovery id
verified := crypto.VerifySignature(publicKeyBytes, hash.Bytes(), signatureNoRecoverID)
fmt.Println(verified) // true
}
查看 Ethereum Development with Go有关使用 go-ethereum 的更多示例的指南。
关于go - 如何从 R || 中的哈希消息和签名正确恢复 ECDSA 公钥S || V格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111605/