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做统计报表 最近做了一个统计项目,这个统计项目大致的需求是统计接口的访问速度.客户端会调用一个接口来记录接口的访问情况,我的需求 ...
随机推荐
- ext4文件系统启动自检的必要性
最近我们发现多个用户设备掉电后重启,系统不工作. 研究这些返修设备,发现这些设备的表象是网络连接失败,DNS resolve不了.进一步发现/etc/resolv.conf为空,所以应用程序没法进行D ...
- 导航贴 | IT Crypt 密码学优秀博文
Base64编码: 什么是Base64? 一篇文章彻底弄懂Base64编码原理 Morse密码: 看似神秘,实则简单的装逼利器-摩斯密码 Bacon 密码: 密码学笔记 -- 培根密码 RSA 加密: ...
- MySQL 8 服务器组件
MySQL 服务器包含了一个基于组件的架构,用于扩展服务器功能.服务器和其他组件可以使用组件提供的服务.(在使用服务方面,服务器也是一个组件,等同于其他组件).组件之间交互仅通过他们各自提供的服务. ...
- 【spring boot】SpringBoot初学(5)– WebService之Jersey
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一.准备 spring boot对jersey1.x与jersey2.x的注入方 ...
- gulp常用插件之gulp-load-plugins使用
更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-load-plugins这是一款批量引入package.json文件中的依赖项工具. 更多使用文档请点击访问gulp-load-plug ...
- JavaDay10(下)
生产者消费者问题 问题描述 有两个进程:一组生产者进程和一组消费者进程共享一个初始为空.固定大小为n的缓存(缓冲区).生产者的工作是制造一段数据,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必 ...
- 2级搭建类202-Oracle 18c SI ASM 静默搭建(OEL7.7)公开
Oracle 18c 单实例 ASM UDEV 方式在 OEL 7.7 上的安装
- C# LINQ学习笔记一:走进LINQ的世界
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5832322.html,记录一下学习过程以备后续查用. LINQ 简介: 语言集成查询(LINQ)是Vi ...
- cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决方法
微信开发的时,请求接口报错如下: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...
- The file is absent or does not have execute permission This file is needed to run this program
tomcat下载后发现startup.sh文件启动不了 原因: 没有权限 解决方案:chmod 777 *.sh