rsa.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package encryption
  2. import (
  3. "crypto"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha256"
  9. "crypto/x509"
  10. "encoding/pem"
  11. "errors"
  12. "io"
  13. )
  14. func RSASign(rsaPrivateKey *rsa.PrivateKey, data []byte) ([]byte, error) {
  15. hashed := sha256.Sum256(data)
  16. return rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey, crypto.SHA256, hashed[:])
  17. }
  18. func VerifySign(rsaPublicKey *rsa.PublicKey, data []byte, sign []byte) error {
  19. hashed := sha256.Sum256(data)
  20. return rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, hashed[:], sign)
  21. }
  22. func AESEncrypt(aesKey []byte, data []byte) ([]byte, error) {
  23. block, err := aes.NewCipher(aesKey)
  24. if err != nil {
  25. return nil, err
  26. }
  27. aesGCM, err := cipher.NewGCM(block)
  28. if err != nil {
  29. return nil, err
  30. }
  31. nonce := make([]byte, aesGCM.NonceSize())
  32. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  33. return nil, err
  34. }
  35. cipherText := aesGCM.Seal(nonce, nonce, data, nil)
  36. return cipherText, nil
  37. }
  38. func AESDecrypt(aesKey []byte, data []byte) ([]byte, error) {
  39. block, err := aes.NewCipher(aesKey)
  40. if err != nil {
  41. return nil, err
  42. }
  43. aesGCM, err := cipher.NewGCM(block)
  44. if err != nil {
  45. return nil, err
  46. }
  47. nonceSize := aesGCM.NonceSize()
  48. if len(data) < nonceSize {
  49. return nil, errors.New("ciphertext too short")
  50. }
  51. nonce, cipherText := data[:nonceSize], data[nonceSize:]
  52. plainText, err := aesGCM.Open(nil, nonce, cipherText, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return plainText, nil
  57. }
  58. func LoadPrivateKey(data []byte) (*rsa.PrivateKey, error) {
  59. private_key_block, rest := pem.Decode(data)
  60. if private_key_block == nil || private_key_block.Type != "RSA PRIVATE KEY" {
  61. return nil, errors.New("failed to decode PEM block containing private key")
  62. }
  63. if len(rest) != 0 {
  64. return nil, errors.New("extra data included in the PEM block")
  65. }
  66. private_key, err := x509.ParsePKCS1PrivateKey(private_key_block.Bytes)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return private_key, nil
  71. }
  72. func LoadPublicKey(data []byte) (*rsa.PublicKey, error) {
  73. public_key_block, rest := pem.Decode(data)
  74. if public_key_block == nil || public_key_block.Type != "RSA PUBLIC KEY" {
  75. return nil, errors.New("failed to decode PEM block containing public key")
  76. }
  77. if len(rest) != 0 {
  78. return nil, errors.New("extra data included in the PEM block")
  79. }
  80. public_key, err := x509.ParsePKCS1PublicKey(public_key_block.Bytes)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return public_key, nil
  85. }