rsa.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package utils
  2. import (
  3. "crypto"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/sha256"
  7. "crypto/x509"
  8. "encoding/pem"
  9. "errors"
  10. "fmt"
  11. )
  12. // RSA公钥私钥产生
  13. func GenRsaKey() (prvkey, pubkey []byte) {
  14. // 生成私钥文件
  15. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  16. if err != nil {
  17. panic(err)
  18. }
  19. derStream := x509.MarshalPKCS1PrivateKey(privateKey)
  20. block := &pem.Block{
  21. Type: "RSA PRIVATE KEY",
  22. Bytes: derStream,
  23. }
  24. prvkey = pem.EncodeToMemory(block)
  25. publicKey := &privateKey.PublicKey
  26. derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
  27. if err != nil {
  28. panic(err)
  29. }
  30. block = &pem.Block{
  31. Type: "PUBLIC KEY",
  32. Bytes: derPkix,
  33. }
  34. pubkey = pem.EncodeToMemory(block)
  35. return
  36. }
  37. // 签名
  38. func RsaSignWithSha256(data []byte, keyBytes []byte) []byte {
  39. h := sha256.New()
  40. h.Write(data)
  41. hashed := h.Sum(nil)
  42. block, _ := pem.Decode(keyBytes)
  43. if block == nil {
  44. panic(errors.New("private key error"))
  45. }
  46. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  47. if err != nil {
  48. fmt.Println("ParsePKCS8PrivateKey err", err)
  49. panic(err)
  50. }
  51. signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
  52. if err != nil {
  53. fmt.Printf("Error from signing: %s\n", err)
  54. panic(err)
  55. }
  56. return signature
  57. }
  58. // 验证
  59. func RsaVerySignWithSha256(data, signData, keyBytes []byte) bool {
  60. block, _ := pem.Decode(keyBytes)
  61. if block == nil {
  62. panic(errors.New("public key error"))
  63. }
  64. pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
  65. if err != nil {
  66. panic(err)
  67. }
  68. hashed := sha256.Sum256(data)
  69. err = rsa.VerifyPKCS1v15(pubKey.(*rsa.PublicKey), crypto.SHA256, hashed[:], signData)
  70. if err != nil {
  71. panic(err)
  72. }
  73. return true
  74. }
  75. // 公钥加密
  76. func RsaEncrypt(data, keyBytes []byte) []byte {
  77. //解密pem格式的公钥
  78. block, _ := pem.Decode(keyBytes)
  79. if block == nil {
  80. panic(errors.New("public key error"))
  81. }
  82. // 解析公钥
  83. pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  84. if err != nil {
  85. panic(err)
  86. }
  87. // 类型断言
  88. pub := pubInterface.(*rsa.PublicKey)
  89. //加密
  90. ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, pub, data)
  91. if err != nil {
  92. panic(err)
  93. }
  94. return ciphertext
  95. }
  96. // 私钥解密
  97. func RsaDecrypt(ciphertext, keyBytes []byte) (data []byte, err error) {
  98. //获取私钥
  99. block, _ := pem.Decode(keyBytes)
  100. if block == nil {
  101. return nil, errors.New("private key error")
  102. }
  103. //解析PKCS1格式的私钥
  104. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  105. if err != nil {
  106. return data, err
  107. }
  108. // 解密
  109. data, err = rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
  110. return data, err
  111. }