From c3842f254870e83758dab69f3e53ea6d6174180e Mon Sep 17 00:00:00 2001 From: Wenhui Shen Date: Sun, 20 Jun 2021 22:22:16 +0800 Subject: [PATCH] update --- rsa.go | 95 ---------------------------------------------- rsa_private_key.go | 88 ++++++++++++++++++++++++++++++++++++++++++ rsa_public_key.go | 88 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 95 deletions(-) create mode 100644 rsa_private_key.go create mode 100644 rsa_public_key.go diff --git a/rsa.go b/rsa.go index 4917553..4f9d081 100644 --- a/rsa.go +++ b/rsa.go @@ -1,11 +1,5 @@ package codec -import ( - "bytes" - "crypto/rsa" - "io/ioutil" -) - func NewRSA() *RSA { return &RSA{} } @@ -32,92 +26,3 @@ func (r *RSA) SetPrivateKey(privKey string) (err error) { func (r *RSA) PrivateKey() *RSAPrivateKey { return r.privateKey } - -type RSAPublicKey struct { - pubStr string //公钥字符串 - pubkey *rsa.PublicKey //公钥 -} - -type RSAPrivateKey struct { - priStr string //私钥字符串 - prikey *rsa.PrivateKey //私钥 -} - -// 设置公钥 -func NewRSAPublicKey(pubStr string) (r *RSAPublicKey, err error) { - r = &RSAPublicKey{} - r.pubStr = pubStr - r.pubkey, err = r.GetPublickey() - return -} - -// 设置私钥 -func NewRSAPrivateKey(priStr string) (r *RSAPrivateKey, err error) { - r = &RSAPrivateKey{} - r.priStr = priStr - r.prikey, err = r.GetPrivatekey() - return -} - -// *rsa.PublicKey -func (r *RSAPrivateKey) GetPrivatekey() (*rsa.PrivateKey, error) { - return getPriKey([]byte(r.priStr)) -} - -// *rsa.PrivateKey -func (r *RSAPublicKey) GetPublickey() (*rsa.PublicKey, error) { - return getPubKey([]byte(r.pubStr)) -} - -// 公钥加密 -func (r *RSAPublicKey) Encrypt(input []byte) ([]byte, error) { - if r.pubkey == nil { - return nil, ErrPublicKeyNotSet - } - output := bytes.NewBuffer(nil) - err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, true) - if err != nil { - return nil, err - } - return ioutil.ReadAll(output) -} - -// 公钥解密 -func (r *RSAPublicKey) Decrypt(input []byte) ([]byte, error) { - if r.pubkey == nil { - return nil, ErrPublicKeyNotSet - } - output := bytes.NewBuffer(nil) - err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, false) - if err != nil { - return nil, err - } - return ioutil.ReadAll(output) -} - -// 私钥加密 -func (rsas *RSAPrivateKey) Encrypt(input []byte) ([]byte, error) { - if rsas.prikey == nil { - return nil, ErrPrivateKeyNotSet - } - output := bytes.NewBuffer(nil) - err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, true) - if err != nil { - return nil, err - } - return ioutil.ReadAll(output) -} - -// 私钥解密 -func (r *RSAPrivateKey) Decrypt(input []byte) ([]byte, error) { - if r.prikey == nil { - return nil, ErrPrivateKeyNotSet - } - output := bytes.NewBuffer(nil) - err := priKeyIO(r.prikey, bytes.NewReader(input), output, false) - if err != nil { - return nil, err - } - - return ioutil.ReadAll(output) -} diff --git a/rsa_private_key.go b/rsa_private_key.go new file mode 100644 index 0000000..d7ffa8a --- /dev/null +++ b/rsa_private_key.go @@ -0,0 +1,88 @@ +package codec + +import ( + "bytes" + "crypto" + "crypto/md5" + "crypto/rand" + "crypto/rsa" + "crypto/sha1" + "crypto/sha256" + "io/ioutil" +) + +// 设置私钥 +func NewRSAPrivateKey(priStr string) (r *RSAPrivateKey, err error) { + r = &RSAPrivateKey{} + r.priStr = priStr + r.prikey, err = r.GetPrivatekey() + return +} + +type RSAPrivateKey struct { + priStr string //私钥字符串 + prikey *rsa.PrivateKey //私钥 +} + +// *rsa.PublicKey +func (r *RSAPrivateKey) GetPrivatekey() (*rsa.PrivateKey, error) { + return getPriKey([]byte(r.priStr)) +} + +// 私钥加密 +func (rsas *RSAPrivateKey) Encrypt(input []byte) ([]byte, error) { + if rsas.prikey == nil { + return nil, ErrPrivateKeyNotSet + } + output := bytes.NewBuffer(nil) + err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, true) + if err != nil { + return nil, err + } + return ioutil.ReadAll(output) +} + +// 私钥解密 +func (r *RSAPrivateKey) Decrypt(input []byte) ([]byte, error) { + if r.prikey == nil { + return nil, ErrPrivateKeyNotSet + } + output := bytes.NewBuffer(nil) + err := priKeyIO(r.prikey, bytes.NewReader(input), output, false) + if err != nil { + return nil, err + } + + return ioutil.ReadAll(output) +} + +/** + * 使用RSAWithMD5算法签名 + */ +func (r *RSAPrivateKey) SignMd5(data []byte) ([]byte, error) { + md5Hash := md5.New() + md5Hash.Write(data) + hashed := md5Hash.Sum(nil) + return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.MD5, hashed) +} + +/** + * 使用RSAWithSHA1算法签名 + */ +func (r *RSAPrivateKey) SignSha1(data []byte) ([]byte, error) { + sha1Hash := sha1.New() + sha1Hash.Write(data) + hashed := sha1Hash.Sum(nil) + return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.SHA1, hashed) +} + +/** + * 使用RSAWithSHA256算法签名 + */ +func (r *RSAPrivateKey) SignSha256(data []byte) ([]byte, error) { + sha256Hash := sha256.New() + s_data := []byte(data) + sha256Hash.Write(s_data) + hashed := sha256Hash.Sum(nil) + return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.SHA256, hashed) +} diff --git a/rsa_public_key.go b/rsa_public_key.go new file mode 100644 index 0000000..28e2f08 --- /dev/null +++ b/rsa_public_key.go @@ -0,0 +1,88 @@ +package codec + +import ( + "bytes" + "crypto" + "crypto/md5" + "crypto/rsa" + "crypto/sha1" + "crypto/sha256" + "encoding/base64" + "io/ioutil" +) + +// 设置公钥 +func NewRSAPublicKey(pubStr string) (r *RSAPublicKey, err error) { + r = &RSAPublicKey{} + r.pubStr = pubStr + r.pubkey, err = r.GetPublickey() + return +} + +type RSAPublicKey struct { + pubStr string //公钥字符串 + pubkey *rsa.PublicKey //公钥 +} + +// *rsa.PrivateKey +func (r *RSAPublicKey) GetPublickey() (*rsa.PublicKey, error) { + return getPubKey([]byte(r.pubStr)) +} + +// 公钥加密 +func (r *RSAPublicKey) Encrypt(input []byte) ([]byte, error) { + if r.pubkey == nil { + return nil, ErrPublicKeyNotSet + } + output := bytes.NewBuffer(nil) + err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, true) + if err != nil { + return nil, err + } + return ioutil.ReadAll(output) +} + +// 公钥解密 +func (r *RSAPublicKey) Decrypt(input []byte) ([]byte, error) { + if r.pubkey == nil { + return nil, ErrPublicKeyNotSet + } + output := bytes.NewBuffer(nil) + err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, false) + if err != nil { + return nil, err + } + return ioutil.ReadAll(output) +} + +/** + * 使用RSAWithMD5验证签名 + */ +func (r *RSAPublicKey) VerifySignMd5(data string, signData string) error { + sign, err := base64.StdEncoding.DecodeString(signData) + if err != nil { + return err + } + hash := md5.New() + hash.Write([]byte(data)) + return rsa.VerifyPKCS1v15(r.pubkey, crypto.MD5, hash.Sum(nil), sign) +} + +/** + * 使用RSAWithSHA1验证签名 + */ +func (r *RSAPublicKey) VerifySignSha1(data []byte, sign []byte) error { + hash := sha1.New() + hash.Write(data) + return rsa.VerifyPKCS1v15(r.pubkey, crypto.SHA1, hash.Sum(nil), sign) +} + +/** + * 使用RSAWithSHA256验证签名 + */ +func (r *RSAPublicKey) VerifySignSha256(data []byte, sign []byte) error { + hash := sha256.New() + hash.Write(data) + + return rsa.VerifyPKCS1v15(r.pubkey, crypto.SHA256, hash.Sum(nil), sign) +}