mygorsa.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/md5"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha1"
  9. "crypto/sha256"
  10. "encoding/base64"
  11. "errors"
  12. "io/ioutil"
  13. )
  14. var RSA = &RSASecurity{}
  15. type RSASecurity struct {
  16. pubStr string //公钥字符串
  17. priStr string //私钥字符串
  18. pubkey *rsa.PublicKey //公钥
  19. prikey *rsa.PrivateKey //私钥
  20. }
  21. // 设置公钥
  22. func (rsas *RSASecurity) SetPublicKey(pubStr string) (err error) {
  23. rsas.pubStr = pubStr
  24. rsas.pubkey, err = rsas.GetPublickey()
  25. return err
  26. }
  27. // 设置私钥
  28. func (rsas *RSASecurity) SetPrivateKey(priStr string) (err error) {
  29. rsas.priStr = priStr
  30. rsas.prikey, err = rsas.GetPrivatekey()
  31. return err
  32. }
  33. // *rsa.PublicKey
  34. func (rsas *RSASecurity) GetPrivatekey() (*rsa.PrivateKey, error) {
  35. return getPriKey([]byte(rsas.priStr))
  36. }
  37. // *rsa.PrivateKey
  38. func (rsas *RSASecurity) GetPublickey() (*rsa.PublicKey, error) {
  39. return getPubKey([]byte(rsas.pubStr))
  40. }
  41. // 公钥加密
  42. func (rsas *RSASecurity) PubKeyENCTYPT(input []byte) ([]byte, error) {
  43. if rsas.pubkey == nil {
  44. return []byte(""), errors.New(`Please set the public key in advance`)
  45. }
  46. output := bytes.NewBuffer(nil)
  47. err := pubKeyIO(rsas.pubkey, bytes.NewReader(input), output, true)
  48. if err != nil {
  49. return []byte(""), err
  50. }
  51. return ioutil.ReadAll(output)
  52. }
  53. // 公钥解密
  54. func (rsas *RSASecurity) PubKeyDECRYPT(input []byte) ([]byte, error) {
  55. if rsas.pubkey == nil {
  56. return []byte(""), errors.New(`Please set the public key in advance`)
  57. }
  58. output := bytes.NewBuffer(nil)
  59. err := pubKeyIO(rsas.pubkey, bytes.NewReader(input), output, false)
  60. if err != nil {
  61. return []byte(""), err
  62. }
  63. return ioutil.ReadAll(output)
  64. }
  65. // 私钥加密
  66. func (rsas *RSASecurity) PriKeyENCTYPT(input []byte) ([]byte, error) {
  67. if rsas.prikey == nil {
  68. return []byte(""), errors.New(`Please set the private key in advance`)
  69. }
  70. output := bytes.NewBuffer(nil)
  71. err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, true)
  72. if err != nil {
  73. return []byte(""), err
  74. }
  75. return ioutil.ReadAll(output)
  76. }
  77. // 私钥解密
  78. func (rsas *RSASecurity) PriKeyDECRYPT(input []byte) ([]byte, error) {
  79. if rsas.prikey == nil {
  80. return []byte(""), errors.New(`Please set the private key in advance`)
  81. }
  82. output := bytes.NewBuffer(nil)
  83. err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, false)
  84. if err != nil {
  85. return []byte(""), err
  86. }
  87. return ioutil.ReadAll(output)
  88. }
  89. /**
  90. * 使用RSAWithMD5算法签名
  91. */
  92. func (rsas *RSASecurity) SignMd5WithRsa(data string) (string, error) {
  93. md5Hash := md5.New()
  94. s_data := []byte(data)
  95. md5Hash.Write(s_data)
  96. hashed := md5Hash.Sum(nil)
  97. signByte, err := rsa.SignPKCS1v15(rand.Reader, rsas.prikey, crypto.MD5, hashed)
  98. sign := base64.StdEncoding.EncodeToString(signByte)
  99. return string(sign), err
  100. }
  101. /**
  102. * 使用RSAWithSHA1算法签名
  103. */
  104. func (rsas *RSASecurity) SignSha1WithRsa(data string) (string, error) {
  105. sha1Hash := sha1.New()
  106. s_data := []byte(data)
  107. sha1Hash.Write(s_data)
  108. hashed := sha1Hash.Sum(nil)
  109. signByte, err := rsa.SignPKCS1v15(rand.Reader, rsas.prikey, crypto.SHA1, hashed)
  110. sign := base64.StdEncoding.EncodeToString(signByte)
  111. return string(sign), err
  112. }
  113. /**
  114. * 使用RSAWithSHA256算法签名
  115. */
  116. func (rsas *RSASecurity) SignSha256WithRsa(data string) (string, error) {
  117. sha256Hash := sha256.New()
  118. s_data := []byte(data)
  119. sha256Hash.Write(s_data)
  120. hashed := sha256Hash.Sum(nil)
  121. signByte, err := rsa.SignPKCS1v15(rand.Reader, rsas.prikey, crypto.SHA256, hashed)
  122. sign := base64.StdEncoding.EncodeToString(signByte)
  123. return string(sign), err
  124. }
  125. /**
  126. * 使用RSAWithMD5验证签名
  127. */
  128. func (rsas *RSASecurity) VerifySignMd5WithRsa(data string, signData string) error {
  129. sign, err := base64.StdEncoding.DecodeString(signData)
  130. if err != nil {
  131. return err
  132. }
  133. hash := md5.New()
  134. hash.Write([]byte(data))
  135. return rsa.VerifyPKCS1v15(rsas.pubkey, crypto.MD5, hash.Sum(nil), sign)
  136. }
  137. /**
  138. * 使用RSAWithSHA1验证签名
  139. */
  140. func (rsas *RSASecurity) VerifySignSha1WithRsa(data string, signData string) error {
  141. sign, err := base64.StdEncoding.DecodeString(signData)
  142. if err != nil {
  143. return err
  144. }
  145. hash := sha1.New()
  146. hash.Write([]byte(data))
  147. return rsa.VerifyPKCS1v15(rsas.pubkey, crypto.SHA1, hash.Sum(nil), sign)
  148. }
  149. /**
  150. * 使用RSAWithSHA256验证签名
  151. */
  152. func (rsas *RSASecurity) VerifySignSha256WithRsa(data string, signData string) error {
  153. sign, err := base64.StdEncoding.DecodeString(signData)
  154. if err != nil {
  155. return err
  156. }
  157. hash := sha256.New()
  158. hash.Write([]byte(data))
  159. return rsa.VerifyPKCS1v15(rsas.pubkey, crypto.SHA256, hash.Sum(nil), sign)
  160. }