go语言 内置的椭圆数字签名及其验证算法
package main import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/big"
) func main() {
// 1、对需要签名的文件进行hash运算
data := "from xiaoxiao to maomao 100 btc"
hashInstance := sha256.New()
hashInstance.Write([]byte(data))
hashed := hashInstance.Sum(nil)
// 2、生成私钥和公钥字节
privateKey, publicKeyBytes := NewKeyPair()
// 3、生成签名的der编码格式
derSignString := ECDSASign(hashed, privateKey)
fmt.Printf("签名信息为:%s\n", derSignString)
// 4、验证签名
flag := ECDSAVerify(publicKeyBytes, hashed, derSignString)
fmt.Println("签名验证结果:", flag)
} // NewKeyPair 生成私钥和公钥,生成的私钥为结构体ecdsa.PrivateKey的指针
func NewKeyPair() (ecdsa.PrivateKey, []byte) {
// 1、生成椭圆曲线对象
curve := elliptic.P256()
// 2、生成秘钥对,返回私钥对象(ecdsa.PrivateKey指针)
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
// 3、编码生成公钥字节数组,参数是椭圆曲线对象、x坐标、y坐标
publicKeyBytes := elliptic.Marshal(curve, privateKey.PublicKey.X, privateKey.Y)
fmt.Printf("私钥:%x\n", *privateKey)
fmt.Printf("公钥:%x\n", publicKeyBytes)
return *privateKey, publicKeyBytes
} // ECDSASign ECDSA数字签名
func ECDSASign(hashed []byte, privateKey ecdsa.PrivateKey) string {
// 1、数字签名生成r、s的big.Int对象,参数是随机数、私钥、签名文件的哈希串
r, s, err := ecdsa.Sign(rand.Reader, &privateKey, hashed)
if err != nil {
return ""
}
fmt.Println("r结果:", r)
fmt.Println("s结果:", s)
// 2、将r、s转成r/s字符串
strSignR := fmt.Sprintf("%x", r)
strSignS := fmt.Sprintf("%x", s)
if len(strSignR) == {
strSignR = "" + strSignR
}
if len(strSignS) == {
strSignS = "" + strSignS
}
fmt.Printf("r的16进制为:%s,长度为:%d\n", strSignR, len(strSignR))
fmt.Printf("s的16进制为:%s,长度为:%d\n", strSignS, len(strSignS))
// 3、r和s字符串拼接,形成数字签名的der格式
derString := MakeDERSignString(strSignR, strSignS)
return derString
} // MakeDERSignString 生成数字签名的DER编码格式
func MakeDERSignString(strR, strS string) string {
// 1、获取R和S的长度
lenSignR := len(strR) /
lenSignS := len(strS) /
// 2、计算DER序列的总长度
len := lenSignR + lenSignS +
fmt.Printf("lenSignR为:%d,lenSignS为:%d,len为:%d\n", lenSignR, lenSignS, len)
// 3、将10进制长度转16进制字符串
strLenSignR := fmt.Sprintf("%x", int64(lenSignR))
strLenSignS := fmt.Sprintf("%x", int64(lenSignS))
strLen := fmt.Sprintf("%x", int64(len))
fmt.Printf("strLenSignR为:%s,strLenSignS为:%s,strLen为:%s\n", strLenSignR, strLenSignS, strLen)
// 4、拼接DER编码格式
derString := "" + strLen
derString += "" + strLenSignR + strR
derString += "" + strLenSignS + strS
derString += ""
return derString
} // ECDSAVerify ECDSA验证签名 (比特币系统中公钥具有0x04前缀)
func ECDSAVerify(publicKeyBytes, hashed []byte, derSignString string) bool {
// 公钥长度
keyLen := len(publicKeyBytes)
if keyLen != {
return false
}
// 1、生成椭圆曲线对象
curve := elliptic.P256()
// 2、根据公钥字节数字,获取公钥中的x和y
// 公钥字节中的前一半为x轴坐标,再将字节数组转成big.Int类型
publicKeyBytes = publicKeyBytes[:]
// x := big.NewInt(0).SetBytes(publicKeyBytes[:32])
x := new(big.Int).SetBytes(publicKeyBytes[:])
y := new(big.Int).SetBytes(publicKeyBytes[:])
// 3、生成公钥对象
publicKey := ecdsa.PublicKey{Curve: curve, X: x, Y: y}
// 4、对der格式的签名进行解析,获取r/s字节数组后转成big.Int类型
rBytes, sBytes := ParseDERSignString(derSignString)
r := new(big.Int).SetBytes(rBytes)
s := new(big.Int).SetBytes(sBytes)
return ecdsa.Verify(&publicKey, hashed, r, s)
} // ParseDERSignString 对der格式的签名进行解析
func ParseDERSignString(derString string) (rBytes, sBytes []byte) {
fmt.Println("derString:", derString)
derBytes, _ := hex.DecodeString(derString)
fmt.Println("derBytes", derBytes)
rBytes = derBytes[:]
sBytes = derBytes[len(derBytes)- : len(derBytes)-]
return
}
go语言 内置的椭圆数字签名及其验证算法的更多相关文章
- 11 The Go Memory Model go语言内置模型
The Go Memory Model go语言内置模型 Version of May 31, 2014 Introduction 介绍 Advice 建议 Happens Before 在发生之前 ...
- GLSL语言内置的变量详解
GLSL语言内置的变量,包括内置的顶点属性(attribute).一致变量(uniform).易变变量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以为以后的编程做查询 ...
- Go语言内置包之strconv
文章引用自 Go语言内置包之strconv Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下 ...
- go语言内置基础类型
1.数值型(Number) 三种:整数型.浮点型和虚数型(有符号整数表示整数范围 -2n-1~2n-1-1:无符号整数表示整数范围 0~2n-1) go内置整型有:uint8, uint16, uin ...
- JavaScript语言内置对象
String(字符串对象)RegExp(正则表达式对象)Number(数字对象)Math(数学对象)Function(函数对象)Error(异常对象)Date(日期/时间对象)Boolean(布尔对象 ...
- Golang(Go语言)内置函数之copy用法
该函数主要是切片(slice)的拷贝,不支持数组 将第二个slice里的元素拷贝到第一个slice里,拷贝的长度为两个slice中长度较小的长度值 示例: s := []int{1,2,3} fmt. ...
- SQL Server 2016将内置R语言?
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:随着大数据成为一个BuzzWord,和大数据相关的技术也变得越来越火热,其中就包括R语 ...
- SQL Server 2016将内置R语言
题记:随着大数据成为一个BuzzWord,和大数据相关的技术也变得越来越火热,其中就包括R语言.而据说SQL Server 2016将会内置R语言支持? R语言作为一个存在很久的语言,在大数据热炒之后 ...
- js中内置有对象
statpot:使用mongo+bootstrap+highcharts做统计报表 最近做了一个统计项目,这个统计项目大致的需求是统计接口的访问速度.客户端会调用一个接口来记录接口的访问情况,我的需求 ...
随机推荐
- cf960F
输入给出m条边,要求找到一条最长的路径满足边按照输入的顺序出现并且权值严格递增 两种方法:第一种利用单调队列性质 第二种利用数据结构优化 #include<bits/stdc++.h> # ...
- javascript生成指定位数的随机数
<script type="text/javascript"> document.write("请输入要生成随机数的位数:"); // digit是 ...
- LAMP搭建随笔
前言 这是我第一次在写博客,里面记录了我配置LAMP遇到的各种各样的细节,也许表述不够准确,希望大佬给于批评指正 环境 OS Ubuntu server 18.04.3 远程连接软件 cmder 文件 ...
- Magento2 观察者模式 之 插件
etc/di.xml 定义如下: <type name="Magento\Quote\Model\Quote\Item\ToOrderItem"> <plugin ...
- Linux内核镜像文件格式与生成过程(转)
<Linux内核镜像格式> Linux内核有多种格式的镜像,包括vmlinux.Image.zImage.bzImage.uImage.xipImage.bootpImage等. ➤k ...
- docker镜像下载出现:received unexpected HTTP status: 500 Internal Server Error
1.镜像下载总是出现报错:received unexpected HTTP status: 500 Internal Server Error 2.尝试多种方法: ①阿里云docke加速器:注册之后, ...
- Spark学习之路 (二十七)图简介[转]
test test test test test test test test test 图 基本概念 图是由顶点集合(vertex)及顶点间的关系集合(边edge)组成的一种数据结构. 这里的图并非 ...
- Rtudio 安装包报错
今天重新安装了一下Rstudio,基本上很多包都安装不上,问了度娘发现被墙了 f..k.. 解决办法,更改安装包的镜像为清华镜像 tools->gloabl options->packag ...
- Wannafly Winter Camp 2020 Day 5J Xor on Figures - 线性基,bitset
有一个\(2^k\cdot 2^k\) 的全零矩阵 \(M\),给出 \(2^k\cdot 2^k\) 的 \(01\) 矩阵 \(F\),现在可以将 \(F\) 的左上角置于 \(M\) 的任一位置 ...
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC 读后感1
2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...