话不多说,公钥也分为压缩公钥和非压缩公钥。 功能代码如下: 测试代码如下: 如果想增加更多的测试用例,可以访问一下下面这个网址: https://learnmeabitcoin.com/guide/public-key (全文完)
非压缩公钥: 0x04 + X + Y 压缩公钥: 如果Y是奇数,压缩公钥为0x03+X;如果Y为偶数,压缩公钥为0x02+X
func NewCompressPubKey(X []byte, Y []byte) string { //前缀03+x(如果y是奇数),前缀02+x(如果y是偶数) var prefix byte if Y[len(Y)-1] %2 != 0 { prefix = 0x3 } else { prefix = 0x2 } var pubKeyInBytes []byte pubKeyInBytes = append([]byte{prefix}, X...) return hex.EncodeToString(pubKeyInBytes) } func NewUncompressPubKey(X []byte, Y []byte) string { //0X04 + X + Y var pubKeyInBytes []byte pubKeyInBytes = append([]byte{0x04}, X...) pubKeyInBytes = append(pubKeyInBytes, Y...) return hex.EncodeToString(pubKeyInBytes) }
import ( "encoding/hex" "github.com/decred/dcrd/dcrec/secp256k1" "testing" ) func TestNewUncompressPubKey(t *testing.T) { var testcases = []struct{ originalPrivKey string compressedPubKey string } { { "5a2d2535d6f40cb64bc76c33551b2a1d7b4c5222893d2dc93590c55128f788b2", "02e92a351210716373fc214b8320c04a50e35f64eba23b92a812c8e52777ae79c5", }, } for _,oneCase := range testcases { privKeyInBytes, _ := hex.DecodeString(oneCase.originalPrivKey) privKey := secp256k1.PrivKeyFromBytes(privKeyInBytes) compressedPubKey := NewCompressPubKey(privKey.PubKey().X().Bytes(), privKey.PubKey().Y().Bytes()) if compressedPubKey != oneCase.compressedPubKey { t.Error("compressed public key error") t.Error("want: ", oneCase.compressedPubKey) t.Error("got: ", compressedPubKey) } } } func TestNewCompressPubKey(t *testing.T) { var testcases = []struct{ originalPrivKey string uncompressedPubKey string } { { "5a2d2535d6f40cb64bc76c33551b2a1d7b4c5222893d2dc93590c55128f788b2", "04e92a351210716373fc214b8320c04a50e35f64eba23b92a812c8e52777ae79c53cc2bec2369a33918f2ae6680ef0ff03056cae8849466556959c8784b07fdc0a", }, } for _,oneCase := range testcases { privKeyInBytes, _ := hex.DecodeString(oneCase.originalPrivKey) privKey := secp256k1.PrivKeyFromBytes(privKeyInBytes) uncompressedPubKey := NewUncompressPubKey(privKey.PubKey().X().Bytes(), privKey.PubKey().Y().Bytes()) if uncompressedPubKey != oneCase.uncompressedPubKey { t.Error("uncompressed public key error") t.Error("want: ", oneCase.uncompressedPubKey) t.Error("got: ", uncompressedPubKey) } } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算