1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

加密算法

This commit is contained in:
2022-04-20 11:34:03 +08:00
parent 0ab7efb7ef
commit cd163ed99b
5 changed files with 31 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package crypto
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
@ -10,6 +11,8 @@ import (
"strconv"
)
// var _ Signer = &ecdsaOptions{}
type ecdsaOptions struct {
hashOptions
prv *ecdsa.PrivateKey
@ -89,12 +92,16 @@ func ParsePublicKey(pem []byte) Option {
return publicKeyOption(pem)
}
func (opt *ecdsaOptions) Sign(msg []byte) ([]byte, error) {
sum := opt.getHash(msg)
return ecdsa.SignASN1(rand.Reader, opt.prv, sum)
}
func (opt *ecdsaOptions) Verify(msg, rText, sText []byte) bool {
var r, s big.Int
_ = r.UnmarshalText(rText)
_ = s.UnmarshalText(sText)
sum := opt.getHash(msg)
return ecdsa.Verify(opt.pub, sum, &r, &s)
}