上代码

使用github.com/farmerx/gorsa

go get github.com/farmerx/gorsa

 


import (
   "crypto"
   "crypto/md5"
   "crypto/rand"
   "crypto/rsa"
   "crypto/sha1"
   "crypto/sha256"
   "encoding/base64"
   "encoding/hex"
   "github.com/farmerx/gorsa"
)

const (
   Base64 = 1
   Hex    = 2
   String = 3
)

type Rsa struct {
   PublicKey  string
   PrivateKey string
}

// 公钥加密 (使用私钥解密)
func (_this *Rsa) PublicKeyEncrypt(originalData []byte) ([]byte, error) {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return []byte{}, err
   }
   return gorsa.RSA.PubKeyENCTYPT(originalData)
}

// 私钥解密 (解密公钥加密的数据)
func (_this *Rsa) PrivateKeyDecrypt(ciphertext []byte) ([]byte, error) {
   err := gorsa.RSA.SetPrivateKey(_this.PrivateKey)
   if err != nil {
      return []byte{}, err
   }
   return gorsa.RSA.PriKeyDECRYPT(ciphertext)
}

// 私钥加密 (使用公钥解密)
func (_this *Rsa) PrivateKeyEncrypt(originalData []byte) ([]byte, error) {
   err := gorsa.RSA.SetPrivateKey(_this.PrivateKey)
   if err != nil {
      return []byte{}, err
   }
   return gorsa.RSA.PriKeyENCTYPT(originalData)
}

// 公钥解密 (解密使用私钥加密的数据)
func (_this *Rsa) PublicKeyDecrypt(ciphertext []byte) ([]byte, error) {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return []byte{}, err
   }
   return gorsa.RSA.PubKeyDECRYPT(ciphertext)
}

// 使用公钥将字符串加密到action 详看 const XXX 默认 base64
func (_this *Rsa) PublicKeyEncryptTo(str string, action int) (string, error) {
   cipher, err := _this.PublicKeyEncrypt([]byte(str))
   switch action {
   case Base64:
      return base64.StdEncoding.EncodeToString(cipher), err
   case Hex:
      return hex.EncodeToString(cipher), err
   case String:
      return string(cipher), err
   default:
      return base64.StdEncoding.EncodeToString(cipher), err
   }
}

// 使用私钥解密 action 到Str 详看 const XXX 默认 base64
func (_this *Rsa) PrivateKeyDecryptTo(ciphertext string, action int) (string, error) {
   var byteCiphertext []byte
   var err error
   switch action {
   case Base64:
      byteCiphertext, err = base64.StdEncoding.DecodeString(ciphertext)
      break
   case Hex:
      byteCiphertext, err = hex.DecodeString(ciphertext)
      break
   case String:
      byteCiphertext = []byte(ciphertext)
      break
   default:
      byteCiphertext, err = base64.StdEncoding.DecodeString(ciphertext)
      break
   }
   if err != nil {
      return "", err
   }
   originalData, err := _this.PrivateKeyDecrypt(byteCiphertext)
   return string(originalData), err
}

// 使用私钥将字符串加密到action 详看 const XXX 默认 base64
func (_this *Rsa) PrivateKeyEncryptTo(str string, action int) (string, error) {
   cipher, err := _this.PrivateKeyEncrypt([]byte(str))
   switch action {
   case Base64:
      return base64.StdEncoding.EncodeToString(cipher), err
   case Hex:
      return hex.EncodeToString(cipher), err
   case String:
      return string(cipher), err
   default:
      return base64.StdEncoding.EncodeToString(cipher), err
   }
}

// 使用公钥解密 action 到Str 详看 const XXX 默认 base64
func (_this *Rsa) PublicKeyDecryptTo(ciphertext string, action int) (string, error) {
   var byteCiphertext []byte
   var err error
   switch action {
   case Base64:
      byteCiphertext, err = base64.StdEncoding.DecodeString(ciphertext)
      break
   case Hex:
      byteCiphertext, err = hex.DecodeString(ciphertext)
      break
   case String:
      byteCiphertext = []byte(ciphertext)
      break
   default:
      byteCiphertext, err = base64.StdEncoding.DecodeString(ciphertext)
      break
   }
   if err != nil {
      return "", err
   }
   originalData, err := _this.PublicKeyDecrypt(byteCiphertext)
   return string(originalData), err
}

// 使用RSAWithMD5算法签名
func (_this *Rsa) SignMd5WithRsa(data string) (string, error) {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return "", err
   }
   PrivateKey, err := gorsa.RSA.GetPrivatekey()
   if err != nil {
      return "", err
   }
   md5Hash := md5.New()
   sData := []byte(data)
   md5Hash.Write(sData)
   hashed := md5Hash.Sum(nil)
   signByte, err := rsa.SignPKCS1v15(rand.Reader, PrivateKey, crypto.MD5, hashed)
   sign := base64.StdEncoding.EncodeToString(signByte)
   return string(sign), err
}

// 使用RSAWithSHA1算法签名
func (_this *Rsa) SignSha1WithRsa(data string) (string, error) {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return "", err
   }
   PrivateKey, err := gorsa.RSA.GetPrivatekey()
   if err != nil {
      return "", err
   }
   sha1Hash := sha1.New()
   sData := []byte(data)
   sha1Hash.Write(sData)
   hashed := sha1Hash.Sum(nil)
   signByte, err := rsa.SignPKCS1v15(rand.Reader, PrivateKey, crypto.SHA1, hashed)
   sign := base64.StdEncoding.EncodeToString(signByte)
   return string(sign), err
}

// 使用RSAWithSHA256算法签名
func (_this *Rsa) SignSha256WithRsa(data string) (string, error) {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return "", err
   }
   PrivateKey, err := gorsa.RSA.GetPrivatekey()
   if err != nil {
      return "", err
   }
   sha256Hash := sha256.New()
   sData := []byte(data)
   sha256Hash.Write(sData)
   hashed := sha256Hash.Sum(nil)
   signByte, err := rsa.SignPKCS1v15(rand.Reader, PrivateKey, crypto.SHA256, hashed)
   sign := base64.StdEncoding.EncodeToString(signByte)
   return string(sign), err
}

// 使用RSAWithMD5验证签名
func (_this *Rsa) VerifySignMd5WithRsa(data string, signData string) error {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return err
   }
   PublicKey, err := gorsa.RSA.GetPublickey()
   if err != nil {
      return err
   }
   sign, err := base64.StdEncoding.DecodeString(signData)
   if err != nil {
      return err
   }
   hash := md5.New()
   hash.Write([]byte(data))
   return rsa.VerifyPKCS1v15(PublicKey, crypto.MD5, hash.Sum(nil), sign)
}

// 使用RSAWithSHA1验证签名
func (_this *Rsa) VerifySignSha1WithRsa(data string, signData string) error {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return err
   }
   PublicKey, err := gorsa.RSA.GetPublickey()
   if err != nil {
      return err
   }
   sign, err := base64.StdEncoding.DecodeString(signData)
   if err != nil {
      return err
   }
   hash := sha1.New()
   hash.Write([]byte(data))
   return rsa.VerifyPKCS1v15(PublicKey, crypto.SHA1, hash.Sum(nil), sign)
}

// 使用RSAWithSHA256验证签名
func (_this *Rsa) VerifySignSha256WithRsa(data string, signData string) error {
   err := gorsa.RSA.SetPublicKey(_this.PublicKey)
   if err != nil {
      return err
   }
   PublicKey, err := gorsa.RSA.GetPublickey()
   if err != nil {
      return err
   }
   sign, err := base64.StdEncoding.DecodeString(signData)
   if err != nil {
      return err
   }
   hash := sha256.New()
   hash.Write([]byte(data))
   return rsa.VerifyPKCS1v15(PublicKey, crypto.SHA256, hash.Sum(nil), sign)
}

 

说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...