md5.go 597 B

12345678910111213141516171819202122232425262728293031323334
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "crypto/rand"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "io"
  8. )
  9. func GetMD5String(strings string) string {
  10. md5Ctx := md5.New()
  11. md5Ctx.Write([]byte(strings))
  12. cipherStr := md5Ctx.Sum(nil)
  13. return hex.EncodeToString(cipherStr)
  14. }
  15. func MD5ByByte(bytes []byte) string {
  16. md5Ctx := md5.New()
  17. md5Ctx.Write(bytes)
  18. cipherStr := md5Ctx.Sum(nil)
  19. return hex.EncodeToString(cipherStr)
  20. }
  21. func UniqueId() string {
  22. b := make([]byte, 48)
  23. if _, err := io.ReadFull(rand.Reader, b); err != nil {
  24. return ""
  25. }
  26. return GetMD5String(base64.URLEncoding.EncodeToString(b))
  27. }