sso

stringtool.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // AES加密
  43. func AESEncrypt(origin string) string {
  44. aes_key := beego.AppConfig.String("aes_key")
  45. xpass, _ := _aesEncrypt([]byte(origin), []byte(aes_key))
  46. pass64 := base64.StdEncoding.EncodeToString(xpass)
  47. return pass64
  48. }
  49. func AESDecrypt(crypted []byte) string {
  50. aes_key := beego.AppConfig.String("aes_key")
  51. origData, err := _aesDecrypt(crypted, []byte(aes_key))
  52. if err != nil {
  53. return ""
  54. }
  55. return string(origData)
  56. }
  57. func _PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  58. padding := blockSize - len(ciphertext)%blockSize
  59. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  60. return append(ciphertext, padtext...)
  61. }
  62. func _PKCS5UnPadding(origData []byte) []byte {
  63. length := len(origData)
  64. unpadding := int(origData[length-1])
  65. return origData[:(length - unpadding)]
  66. }
  67. func _aesEncrypt(origData, key []byte) ([]byte, error) {
  68. block, err := aes.NewCipher(key)
  69. if err != nil {
  70. return nil, err
  71. }
  72. blockSize := block.BlockSize()
  73. origData = _PKCS5Padding(origData, blockSize)
  74. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  75. crypted := make([]byte, len(origData))
  76. blockMode.CryptBlocks(crypted, origData)
  77. return crypted, nil
  78. }
  79. func _aesDecrypt(crypted, key []byte) ([]byte, error) {
  80. block, err := aes.NewCipher(key)
  81. if err != nil {
  82. return nil, err
  83. }
  84. blockSize := block.BlockSize()
  85. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  86. origData := make([]byte, len(crypted))
  87. blockMode.CryptBlocks(origData, crypted)
  88. origData = _PKCS5UnPadding(origData)
  89. return origData, nil
  90. }