登录
首页 >  Golang >  Go问答

btcec 库是如何验证 secp256k1 签名的?

来源:stackoverflow

时间:2024-02-07 11:06:16 480浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《btcec 库是如何验证 secp256k1 签名的?》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在使用 btcec 库在 Go 中处理 secp256k1 签名。不过我在官方文档中并没有找到明确的验证签名的方法。btcec 文档中有一个“验证签名”示例的链接,但似乎没有直接提供示例代码。

我想知道,btcec库中的哪个方法用于验证secp256k1签名?如果有人可以提供一个简单的代码示例,那就太好了。谢谢!


正确答案


给你;-)

https://github.com/btcsuite/btcd /blob/master/btcec/ecdsa/example_test.go

// This example demonstrates verifying a secp256k1 signature against a public
// key that is first parsed from raw bytes.  The signature is also parsed from
// raw bytes.
func Example_verifySignature() {
    // Decode hex-encoded serialized public key.
    pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c" +
        "6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5")
    if err != nil {
        fmt.Println(err)
        return
    }
    pubKey, err := btcec.ParsePubKey(pubKeyBytes)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Decode hex-encoded serialized signature.
    sigBytes, err := hex.DecodeString("30450220090ebfb3690a0ff115bb1b38b" +
        "8b323a667b7653454f1bccb06d4bbdca42c2079022100ec95778b51e707" +
        "1cb1205f8bde9af6592fc978b0452dafe599481c46d6b2e479")

    if err != nil {
        fmt.Println(err)
        return
    }
    signature, err := ecdsa.ParseSignature(sigBytes)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Verify the signature for the message using the public key.
    message := "test message"
    messageHash := chainhash.DoubleHashB([]byte(message))
    verified := signature.Verify(messageHash, pubKey)
    fmt.Println("Signature Verified?", verified)

    // Output:
    // Signature Verified? true
}

今天关于《btcec 库是如何验证 secp256k1 签名的?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>