encoding.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package encoding
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5"
  7. "crypto/sha1"
  8. "crypto/sha256"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "fmt"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. )
  14. func AESEncrypt(originData string, key string) (string, error) {
  15. if strutils.IsStringEmpty(originData) {
  16. return "", nil
  17. }
  18. if strutils.IsStringEmpty(key) {
  19. return originData, nil
  20. }
  21. keyBytes := []byte(key)
  22. block, err := aes.NewCipher(keyBytes)
  23. if err != nil {
  24. return "", err
  25. }
  26. originDataBytes := []byte(originData)
  27. blockSize := block.BlockSize()
  28. originDataBytes = pkcs7Padding(originDataBytes, blockSize)
  29. blockMode := cipher.NewCBCEncrypter(block, keyBytes[:blockSize])
  30. encrypted := make([]byte, len(originDataBytes))
  31. blockMode.CryptBlocks(encrypted, originDataBytes)
  32. return base64.StdEncoding.EncodeToString(encrypted), nil
  33. }
  34. func AESDecrypt(encrypted string, key string) (string, error) {
  35. if strutils.IsStringEmpty(encrypted) {
  36. return "", nil
  37. }
  38. if strutils.IsStringEmpty(key) {
  39. return encrypted, nil
  40. }
  41. keyBytes := []byte(key)
  42. block, err := aes.NewCipher(keyBytes)
  43. if err != nil {
  44. return "", err
  45. }
  46. decoded, err := base64.StdEncoding.DecodeString(encrypted)
  47. if err != nil {
  48. return "", err
  49. }
  50. blockSize := block.BlockSize()
  51. blockMode := cipher.NewCBCDecrypter(block, keyBytes[:blockSize])
  52. originData := make([]byte, len(decoded))
  53. blockMode.CryptBlocks(originData, decoded)
  54. originData = pkcs7UnPadding(originData)
  55. return string(originData), nil
  56. }
  57. func MD5(origin string, salt string) string {
  58. if salt != "" {
  59. origin = origin + salt
  60. }
  61. return fmt.Sprintf("%x", md5.New().Sum([]byte(origin)))
  62. }
  63. func SHA256(origin string, salt string) string {
  64. if salt != "" {
  65. origin = origin + salt
  66. }
  67. return fmt.Sprintf("%x", sha256.Sum256([]byte(origin)))
  68. }
  69. func SHA1(origin string, salt string) string {
  70. if salt != "" {
  71. origin = origin + salt
  72. }
  73. o := sha1.New()
  74. o.Write([]byte(origin))
  75. return hex.EncodeToString(o.Sum(nil))
  76. }
  77. func Base64Encode(content []byte) string {
  78. return base64.StdEncoding.EncodeToString(content)
  79. }
  80. func Base64Decode(content string) ([]byte, error) {
  81. return base64.StdEncoding.DecodeString(content)
  82. }
  83. func pkcs7Padding(ciphertext []byte, blockSize int) []byte {
  84. padding := blockSize - len(ciphertext)%blockSize
  85. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  86. return append(ciphertext, padText...)
  87. }
  88. func pkcs7UnPadding(originData []byte) []byte {
  89. length := len(originData)
  90. unPadding := int(originData[length-1])
  91. return originData[:(length - unPadding)]
  92. }