encrypt.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package utility
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5"
  7. "crypto/rand"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "fmt"
  11. "golang.org/x/crypto/sha3"
  12. "io"
  13. "strings"
  14. )
  15. // 암호화
  16. var key = []byte{0x17, 0xc0, 0xcc, 0x00, 0x32, 0x88, 0x11, 0xa1, 0x51, 0xfe, 0xff, 0x81, 0x9c, 0xdc, 0x9f, 0xea, 0x60, 0x2f, 0x71, 0x28, 0x16, 0x1f, 0x41, 0x7a, 0xa5, 0xc4, 0xac, 0xdd, 0x50, 0x78, 0x08, 0x3f}
  17. func HashPassword(pw string) string {
  18. bin := sha3.Sum256([]byte(pw))
  19. str := hex.EncodeToString(bin[:])
  20. return str
  21. }
  22. func Encrypt(buff []byte) ([]byte, error) {
  23. block, err := aes.NewCipher(key)
  24. if err != nil {
  25. return nil, err
  26. }
  27. ciphertext := make([]byte, aes.BlockSize+len(buff))
  28. iv := ciphertext[:aes.BlockSize]
  29. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  30. return nil, err
  31. }
  32. cfb := cipher.NewCFBEncrypter(block, iv)
  33. cfb.XORKeyStream(ciphertext[aes.BlockSize:], buff)
  34. return ciphertext, nil
  35. }
  36. func EncryptString(s string) (string, error) {
  37. result, err := Encrypt([]byte(s))
  38. if err != nil {
  39. return "", err
  40. }
  41. return hex.EncodeToString(result), nil
  42. }
  43. func Decrypt(buff []byte) ([]byte, error) {
  44. block, err := aes.NewCipher(key)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if len(buff) < aes.BlockSize {
  49. return nil, fmt.Errorf("ciphertext too short")
  50. }
  51. iv := buff[:aes.BlockSize]
  52. buff = buff[aes.BlockSize:]
  53. cfb := cipher.NewCFBDecrypter(block, iv)
  54. cfb.XORKeyStream(buff, buff)
  55. return buff, nil
  56. }
  57. func DecryptString(s string) (string, error) {
  58. buf, err := hex.DecodeString(s)
  59. if err != nil {
  60. return "", err
  61. }
  62. result, err := Decrypt(buf)
  63. if err != nil {
  64. return "", err
  65. }
  66. return string(result), nil
  67. }
  68. func MakeMD5(text string) string {
  69. hasher := md5.New()
  70. hasher.Write([]byte(text))
  71. return string(hasher.Sum(nil))
  72. }
  73. func AesEncrypt(plainText string, key string, iv string) (string, error) {
  74. if strings.TrimSpace(plainText) == "" {
  75. return plainText, nil
  76. }
  77. block, err := aes.NewCipher([]byte(key))
  78. if err != nil {
  79. return "", err
  80. }
  81. encrypter := cipher.NewCBCEncrypter(block, []byte(iv))
  82. paddedPlainText := padPKCS7([]byte(plainText), encrypter.BlockSize())
  83. cipherText := make([]byte, len(paddedPlainText))
  84. // CryptBlocks 함수에 데이터(paddedPlainText)와 암호화 될 데이터를 저장할 슬라이스(cipherText)를 넣으면 암호화가 된다.
  85. encrypter.CryptBlocks(cipherText, paddedPlainText)
  86. return base64.StdEncoding.EncodeToString(cipherText), nil
  87. }
  88. func AesDecrypt(cipherText string, key string, iv string) (string, error) {
  89. if strings.TrimSpace(cipherText) == "" {
  90. return cipherText, nil
  91. }
  92. decodedCipherText, err := base64.StdEncoding.DecodeString(cipherText)
  93. if err != nil {
  94. return "", err
  95. }
  96. block, err := aes.NewCipher([]byte(key))
  97. if err != nil {
  98. return "", err
  99. }
  100. decrypter := cipher.NewCBCDecrypter(block, []byte(iv))
  101. plainText := make([]byte, len(decodedCipherText))
  102. decrypter.CryptBlocks(plainText, decodedCipherText)
  103. trimmedPlainText := trimPKCS5(plainText)
  104. return string(trimmedPlainText), nil
  105. }
  106. func padPKCS7(plainText []byte, blockSize int) []byte {
  107. padding := blockSize - len(plainText)%blockSize
  108. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  109. return append(plainText, padText...)
  110. }
  111. func trimPKCS5(text []byte) []byte {
  112. padding := text[len(text)-1]
  113. return text[:len(text)-int(padding)]
  114. }