package main
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"crypto/elliptic"
"log"
"fmt"
)
//生成私钥和公钥,生成的私钥为结构体ecdsa.PrivateKey的指针
//type PrivateKey struct {
// PublicKey
// D *big.Int
//}
func newKeyPair2() (ecdsa.PrivateKey, []byte) {
//生成secp256k1椭圆曲线
curve := elliptic.P256()
//产生的是一个结构体指针,结构体类型为ecdsa.PrivateKey
private, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
//x坐标与y坐标拼接在一起,生成公钥
pubKey := append(private.PublicKey.X.Bytes(), private.PublicKey.Y.Bytes()...)
return *private, pubKey
}
func main(){
//调用函数生成私钥与公钥
privKey,_ := newKeyPair2()
//信息的哈希,签名什么样的数据
hash := sha256.Sum256([]byte("hello world\n"))
//根据私钥和信息的哈希进行数字签名,产生r和s
r, s, err := ecdsa.Sign(rand.Reader, &privKey, hash[:])
if err != nil {
log.Panic(err)
}
//r和s拼接在一起实现了数字签名
signature := append(r.Bytes(), s.Bytes()...)
//打印数字签名的16进制显示
fmt.Printf("%x\n", signature)
fmt.Printf("%x\n", r.Bytes())
fmt.Printf("%x\n", s.Bytes())
//补充:如何把一个字符串转换为16进制数据
//m := big.Int{}
//n := big.Int{}
//rr,_:=hex.DecodeString("7dccc0f58639584a3f0c879c3688d2f4a0137697cbf34245d075c764e36233d2")
//ss,_:=hex.DecodeString("cf3713bf4369eb1c02e476cdbefb7f76a25b572f53fb71d4e4742fa11c827526")
//
//m.SetBytes(rr)
//n.SetBytes(ss)
//
//fmt.Printf("%x\n", m.Bytes())
//fmt.Printf("%x\n", n.Bytes())
}
-
本文链接: https://dreamerjonson.com/2018/12/07/golang-36-blockchain-signature/
-
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 如何为区块链生成公钥和私钥
- golang[35]-区块链-私钥公钥生成
- 兄弟连区块链教程以太坊源码分析以太坊随机数生成方式一
- 区块链技术+区块链怎么赚钱?
- 区块链技术入门:区块链是什么
- 阿里申请可“行政干预”区块链专利,区块链变味?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Cracking the Coding Interview
Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95
Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!