分销商城(微商城)接口项目

stringtool.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/md5"
  7. "encoding/base64"
  8. "fmt"
  9. "math/rand"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/astaxie/beego"
  14. )
  15. // 将字符串加密成 md5
  16. func String2md5(str string) string {
  17. data := []byte(str)
  18. has := md5.Sum(data)
  19. return fmt.Sprintf("%x", has) //将[]byte转成16进制
  20. }
  21. // RandomString 在数字、大写字母、小写字母范围内生成num位的随机字符串
  22. func RandomString(length int) string {
  23. // 48 ~ 57 数字
  24. // 65 ~ 90 A ~ Z
  25. // 97 ~ 122 a ~ z
  26. // 一共62个字符,在0~61进行随机,小于10时,在数字范围随机,
  27. // 小于36在大写范围内随机,其他在小写范围随机
  28. rand.Seed(time.Now().UnixNano())
  29. result := make([]string, 0, length)
  30. for i := 0; i < length; i++ {
  31. t := rand.Intn(62)
  32. if t < 10 {
  33. result = append(result, strconv.Itoa(rand.Intn(10)))
  34. } else if t < 36 {
  35. result = append(result, string(rand.Intn(26)+65))
  36. } else {
  37. result = append(result, string(rand.Intn(26)+97))
  38. }
  39. }
  40. return strings.Join(result, "")
  41. }
  42. func RandomNumberString(length int) string {
  43. var str string
  44. for i := 0; i < length; i++ {
  45. rand.Seed(time.Now().UnixNano())
  46. str += strconv.Itoa(rand.Intn(10))
  47. }
  48. return str
  49. }
  50. // AES加密
  51. func AESEncrypt(origin string) string {
  52. aes_key := beego.AppConfig.String("aes_key")
  53. fmt.Println(aes_key)
  54. xpass, _ := _aesEncrypt([]byte(origin), []byte(aes_key))
  55. fmt.Println(_aesEncrypt([]byte(origin), []byte(aes_key)))
  56. fmt.Println(xpass)
  57. pass64 := base64.StdEncoding.EncodeToString(xpass)
  58. fmt.Println(pass64)
  59. return pass64
  60. }
  61. func AESDecrypt(crypted []byte) string {
  62. aes_key := beego.AppConfig.String("aes_key")
  63. origData, err := _aesDecrypt(crypted, []byte(aes_key))
  64. if err != nil {
  65. return ""
  66. }
  67. return string(origData)
  68. }
  69. func _PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  70. padding := blockSize - len(ciphertext)%blockSize
  71. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  72. return append(ciphertext, padtext...)
  73. }
  74. func _PKCS5UnPadding(origData []byte) []byte {
  75. length := len(origData)
  76. unpadding := int(origData[length-1])
  77. return origData[:(length - unpadding)]
  78. }
  79. func _aesEncrypt(origData, key []byte) ([]byte, error) {
  80. block, err := aes.NewCipher(key)
  81. if err != nil {
  82. return nil, err
  83. }
  84. blockSize := block.BlockSize()
  85. origData = _PKCS5Padding(origData, blockSize)
  86. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  87. crypted := make([]byte, len(origData))
  88. blockMode.CryptBlocks(crypted, origData)
  89. return crypted, nil
  90. }
  91. func _aesDecrypt(crypted, key []byte) ([]byte, error) {
  92. block, err := aes.NewCipher(key)
  93. if err != nil {
  94. return nil, err
  95. }
  96. blockSize := block.BlockSize()
  97. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  98. origData := make([]byte, len(crypted))
  99. blockMode.CryptBlocks(origData, crypted)
  100. origData = _PKCS5UnPadding(origData)
  101. return origData, nil
  102. }