libString.go 799 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package lib
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "math/rand"
  6. "strings"
  7. "time"
  8. )
  9. // #取得随机字符串:通过打乱slice来操作
  10. func GetRandstring(length int, char string, rand_x int64) string {
  11. if length < 1 {
  12. return ""
  13. }
  14. if len(char) <= 6 || len(char) <= length {
  15. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  16. }
  17. charArr := strings.Split(char, "")
  18. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  19. l := len(charArr)
  20. for i := l - 1; i > 0; i-- {
  21. r := ran.Intn(i)
  22. charArr[r], charArr[i] = charArr[i], charArr[r]
  23. }
  24. rchar := charArr[:length]
  25. return strings.Join(rchar, "")
  26. }
  27. // 返回一个32位md5加密后的字符串
  28. func Md5(str string) string {
  29. h := md5.New()
  30. h.Write([]byte(str))
  31. return hex.EncodeToString(h.Sum(nil))
  32. }