mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
加密算法
This commit is contained in:
18
crypto/crypto.go
Normal file
18
crypto/crypto.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
// 对称加密
|
||||||
|
type ISymmetric interface {
|
||||||
|
Encrypt(msg []byte) ([]byte, error)
|
||||||
|
Decrypt(cipherText []byte) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 非对称加密
|
||||||
|
type IAsymmetric interface {
|
||||||
|
Encrypt(msg []byte) ([]byte, error)
|
||||||
|
Decrypt(ciphertext []byte) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Signer interface {
|
||||||
|
Sign(msg []byte) ([]byte, error)
|
||||||
|
Verify(msg, sign []byte) bool
|
||||||
|
}
|
@ -3,6 +3,7 @@ package crypto
|
|||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
|
"crypto/rand"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
@ -10,6 +11,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// var _ Signer = &ecdsaOptions{}
|
||||||
|
|
||||||
type ecdsaOptions struct {
|
type ecdsaOptions struct {
|
||||||
hashOptions
|
hashOptions
|
||||||
prv *ecdsa.PrivateKey
|
prv *ecdsa.PrivateKey
|
||||||
@ -89,12 +92,16 @@ func ParsePublicKey(pem []byte) Option {
|
|||||||
return publicKeyOption(pem)
|
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 {
|
func (opt *ecdsaOptions) Verify(msg, rText, sText []byte) bool {
|
||||||
var r, s big.Int
|
var r, s big.Int
|
||||||
_ = r.UnmarshalText(rText)
|
_ = r.UnmarshalText(rText)
|
||||||
_ = s.UnmarshalText(sText)
|
_ = s.UnmarshalText(sText)
|
||||||
|
|
||||||
sum := opt.getHash(msg)
|
sum := opt.getHash(msg)
|
||||||
|
|
||||||
return ecdsa.Verify(opt.pub, sum, &r, &s)
|
return ecdsa.Verify(opt.pub, sum, &r, &s)
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ IAsymmetric = &rsaInstance{}
|
||||||
|
|
||||||
type rsaInstance struct {
|
type rsaInstance struct {
|
||||||
hashOptions
|
hashOptions
|
||||||
|
@ -15,6 +15,8 @@ var (
|
|||||||
C1C2C3 = 1
|
C1C2C3 = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ IAsymmetric = &sm2Instance{}
|
||||||
|
|
||||||
type sm2Instance struct {
|
type sm2Instance struct {
|
||||||
mode int
|
mode int
|
||||||
prk *s.PrivateKey
|
prk *s.PrivateKey
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"github.com/tjfoc/gmsm/sm4"
|
"github.com/tjfoc/gmsm/sm4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ ISymmetric = &sm4EcbInstance{}
|
||||||
|
|
||||||
type sm4Instance struct {
|
type sm4Instance struct {
|
||||||
key []byte
|
key []byte
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user