PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/infrastructure/utils/string.go

https://bitbucket.org/behmaroman/ro_be
Go | 42 lines | 35 code | 7 blank | 0 comment | 4 complexity | 0ebfdf1a8e088e4776cf79523f7b9bd0 MD5 | raw file
  1. package utils
  2. import (
  3. "encoding/base64"
  4. "crypto/rand"
  5. "io"
  6. "github.com/google/uuid"
  7. "fmt"
  8. "crypto/md5"
  9. )
  10. const (
  11. EmailRegexp = `^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`
  12. )
  13. func SID() string {
  14. b := make([]byte, 32)
  15. if _, err := io.ReadFull(rand.Reader, b); err != nil {
  16. return ""
  17. }
  18. return base64.URLEncoding.EncodeToString(b)
  19. }
  20. func UUID() string {
  21. return uuid.New().String()
  22. }
  23. func MD5(st string) string {
  24. return fmt.Sprintf("%x", md5.Sum([]byte(st)))
  25. }
  26. func Base64Encode(str string) string {
  27. return base64.StdEncoding.EncodeToString([]byte(str))
  28. }
  29. func Base64Decode(str string) string {
  30. decoded, err := base64.StdEncoding.DecodeString(str)
  31. if err != nil {
  32. return ""
  33. }
  34. return string(decoded)
  35. }