mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
加密包装
This commit is contained in:
100
crypto/ecdsa.go
Normal file
100
crypto/ecdsa.go
Normal file
@ -0,0 +1,100 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"math/big"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ecdsaOptions struct {
|
||||
hashOptions
|
||||
prv *ecdsa.PrivateKey
|
||||
pub *ecdsa.PublicKey
|
||||
}
|
||||
|
||||
type Option interface {
|
||||
apply(*ecdsaOptions) error
|
||||
}
|
||||
|
||||
type hash2Option interface {
|
||||
apply(*hashOptions) error
|
||||
}
|
||||
|
||||
type privateKeyOption []byte
|
||||
|
||||
func (p privateKeyOption) apply(opts *ecdsaOptions) error {
|
||||
block, _ := pem.Decode(p)
|
||||
prv, err := x509.ParseECPrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.prv = prv
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type publicKeyOption []byte
|
||||
|
||||
func (p publicKeyOption) apply(opts *ecdsaOptions) error {
|
||||
block, _ := pem.Decode(p)
|
||||
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.pub = pub.(*ecdsa.PublicKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
type hashOption crypto.Hash
|
||||
|
||||
func (p hashOption) apply(opts *hashOptions) error {
|
||||
opts.h = crypto.Hash(p)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewEcdsa(h Hash, opts ...Option) (*ecdsaOptions, error) {
|
||||
i := &ecdsaOptions{}
|
||||
|
||||
sh := crypto.Hash(h)
|
||||
if !sh.Available() {
|
||||
return nil, errors.New("unknown hash value " + strconv.Itoa(int(h)))
|
||||
}
|
||||
|
||||
i.h = sh
|
||||
|
||||
for _, v := range opts {
|
||||
if err := v.apply(i); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func WithHash2(h Hash) hash2Option {
|
||||
return hashOption(h)
|
||||
}
|
||||
|
||||
func ParsePrivateKey(pem []byte) Option {
|
||||
return privateKeyOption(pem)
|
||||
}
|
||||
|
||||
func ParsePublicKey(pem []byte) Option {
|
||||
return publicKeyOption(pem)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user