Files
goEncrypt/EccSign.go
songguangy 145c388794 GO加密包
2018-11-04 19:33:59 +08:00

94 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package wmgocrypt
import (
"crypto/sha256"
"crypto/rand"
"encoding/pem"
"crypto/x509"
"log"
"runtime"
"crypto/ecdsa"
"math/big"
)
/*
@Time : 2018/11/4 18:51
@Author : wuman
@File : EccSign
@Software: GoLand
*/
func init(){
log.SetFlags(log.Ldate|log.Lshortfile)
} //处理日志的格式
func EccSign(msg []byte,Key []byte)([]byte,[]byte){
// 获取私钥
//2. pem格式解码
block, _ := pem.Decode(Key)
//防止用户传的密钥不正确导致panic,这里恢复程序并打印错误
defer func(){
if err:=recover();err!=nil{
switch err.(type){
case runtime.Error:
log.Println("runtime err:",err,"请检查密钥是否正确")
default:
log.Println("error:",err)
}
}
}()
//3.x509解码
privateKey, _ := x509.ParseECPrivateKey(block.Bytes)
/*if err!=nil{
return []byte{},err
}*/
// 计算消息的hash值
myhash := sha256.New()
myhash.Write(msg)
resultHash := myhash.Sum(nil)
//使用私钥对任意长度的hash值必须是较大信息的hash结果进行签名返回签名结果一对大整数
r, s, _ := ecdsa.Sign(rand.Reader, privateKey, resultHash)
rText, _ := r.MarshalText()
sText, _ := s.MarshalText()
return rText,sText
}
func EccVerifySign(msg []byte,Key []byte,rText,sText []byte) bool {
// 获取公钥
//1. pem格式解码
block, _ := pem.Decode(Key)
//防止用户传的密钥不正确导致panic,这里恢复程序并打印错误
defer func(){
if err:=recover();err!=nil{
switch err.(type){
case runtime.Error:
log.Println("runtime err:",err,"请检查密钥是否正确")
default:
log.Println("error:",err)
}
}
}()
// x509解码
publicKeyInterface, _ := x509.ParsePKIXPublicKey(block.Bytes)
publicKey:=publicKeyInterface.(*ecdsa.PublicKey) //返回的接口类型需要类型断言一下
myhash := sha256.New()
myhash.Write(msg)
resultHash := myhash.Sum(nil)
var r,s big.Int
r.UnmarshalText(rText)
s.UnmarshalText(sText)
// 校验签名
result := ecdsa.Verify(publicKey, resultHash, &r, &s)
return result
}