rsa.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package myth
  2. import (
  3. "crypto"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/sha256"
  7. "crypto/sha512"
  8. "crypto/x509"
  9. "encoding/pem"
  10. "errors"
  11. )
  12. // RSA
  13. type RSA struct {
  14. publicKey *rsa.PublicKey
  15. privateKey *rsa.PrivateKey
  16. }
  17. // NewRSA new rsa
  18. func NewRSA() *RSA {
  19. return &RSA{}
  20. }
  21. // RSA 公、私钥生成
  22. func RSAGenerateKeys() (privatePEM, publicPEM []byte, err error) {
  23. var privateKey *rsa.PrivateKey
  24. privateKey, err = rsa.GenerateKey(rand.Reader, 2048)
  25. if err != nil {
  26. return
  27. }
  28. // Encode private key to PEM format
  29. privatePEM = pem.EncodeToMemory(&pem.Block{
  30. Type: "RSA PRIVATE KEY",
  31. Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
  32. })
  33. // Encode public key to PEM format
  34. var pubASN1 []byte
  35. pubASN1, err = x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
  36. if err != nil {
  37. return
  38. }
  39. publicPEM = pem.EncodeToMemory(&pem.Block{
  40. Type: "RSA PUBLIC KEY",
  41. Bytes: pubASN1,
  42. })
  43. return
  44. }
  45. // 载入 公钥
  46. func (r *RSA) LoadPublicKey(data []byte) (err error) {
  47. var block *pem.Block
  48. block, _ = pem.Decode(data)
  49. if block == nil {
  50. err = errors.New("decode public key error")
  51. return
  52. }
  53. var k any
  54. k, err = x509.ParsePKIXPublicKey(block.Bytes)
  55. if err != nil {
  56. return
  57. }
  58. r.publicKey = k.(*rsa.PublicKey)
  59. return
  60. }
  61. // 载入 私钥
  62. func (r *RSA) LoadPrivateKey(data []byte) (err error) {
  63. var block *pem.Block
  64. block, _ = pem.Decode(data)
  65. if block == nil {
  66. err = errors.New("decode private key error")
  67. return
  68. }
  69. r.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
  70. return
  71. }
  72. /*
  73. // Encrypt encrypts data using rsa public key.
  74. func (r *RSA) Encrypt(msg, label []byte) ([]byte, error) {
  75. return rsa.EncryptOAEP(sha256.New(), rand.Reader, r.publicKey, msg, label)
  76. }
  77. // Encrypt encrypts data using rsa public key.
  78. func (r *RSA) Decrypt(ciphertext, label []byte) ([]byte, error) {
  79. return rsa.DecryptOAEP(sha256.New(), rand.Reader, r.privateKey, ciphertext, label)
  80. }
  81. // */
  82. // Sign signs using private key with sha256
  83. func (r *RSA) Sign(data []byte) (signs []byte, err error) {
  84. h := sha256.Sum256(data)
  85. signs, err = rsa.SignPKCS1v15(rand.Reader, r.privateKey, crypto.SHA256, h[:])
  86. return
  87. }
  88. // Sign256 signs using private key with sha512
  89. func (r *RSA) Sign512(data []byte) (signs []byte, err error) {
  90. h := sha512.Sum512(data)
  91. signs, err = rsa.SignPKCS1v15(rand.Reader, r.privateKey, crypto.SHA512, h[:])
  92. return
  93. }
  94. // verifies signature using public key with sha256
  95. func (r *RSA) Verify256(data, signs []byte) (err error) {
  96. h := sha256.Sum256(data)
  97. err = rsa.VerifyPKCS1v15(r.publicKey, crypto.SHA256, h[:], signs)
  98. return
  99. }
  100. // verifies signature using public key with sha512
  101. func (r *RSA) Verify512(data, signs []byte) (err error) {
  102. h := sha256.Sum256(data)
  103. err = rsa.VerifyPKCS1v15(r.publicKey, crypto.SHA256, h[:], signs)
  104. return
  105. }