Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jun 20, 2021
1 parent d08d49b commit c3842f2
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 95 deletions.
95 changes: 0 additions & 95 deletions rsa.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package codec

import (
"bytes"
"crypto/rsa"
"io/ioutil"
)

func NewRSA() *RSA {
return &RSA{}
}
Expand All @@ -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)
}
88 changes: 88 additions & 0 deletions rsa_private_key.go
Original file line number Diff line number Diff line change
@@ -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)
}
88 changes: 88 additions & 0 deletions rsa_public_key.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c3842f2

Please sign in to comment.