golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 line
2.3 KiB

  1. package utils
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "regexp"
  6. )
  7. const (
  8. Base64Std = iota
  9. Base64Url
  10. Base64RawStd
  11. Base64RawUrl
  12. )
  13. func Base64StdEncode(str interface{}) string {
  14. return Base64Encode(str, Base64Std)
  15. }
  16. func Base64StdDecode(str interface{}) string {
  17. return Base64Decode(str, Base64Std)
  18. }
  19. func Base64UrlEncode(str interface{}) string {
  20. return Base64Encode(str, Base64Url)
  21. }
  22. func Base64UrlDecode(str interface{}) string {
  23. return Base64Decode(str, Base64Url)
  24. }
  25. func Base64RawStdEncode(str interface{}) string {
  26. return Base64Encode(str, Base64RawStd)
  27. }
  28. func Base64RawStdDecode(str interface{}) string {
  29. return Base64Decode(str, Base64RawStd)
  30. }
  31. func Base64RawUrlEncode(str interface{}) string {
  32. return Base64Encode(str, Base64RawUrl)
  33. }
  34. func Base64RawUrlDecode(str interface{}) string {
  35. return Base64Decode(str, Base64RawUrl)
  36. }
  37. func Base64Encode(str interface{}, encode int) string {
  38. newEncode := base64Encode(encode)
  39. if newEncode == nil {
  40. return ""
  41. }
  42. switch v := str.(type) {
  43. case string:
  44. return newEncode.EncodeToString([]byte(v))
  45. case []byte:
  46. return newEncode.EncodeToString(v)
  47. }
  48. return newEncode.EncodeToString([]byte(fmt.Sprint(str)))
  49. }
  50. func Base64Decode(str interface{}, encode int) string {
  51. var err error
  52. var b []byte
  53. newEncode := base64Encode(encode)
  54. if newEncode == nil {
  55. return ""
  56. }
  57. switch v := str.(type) {
  58. case string:
  59. b, err = newEncode.DecodeString(v)
  60. case []byte:
  61. b, err = newEncode.DecodeString(string(v))
  62. default:
  63. return ""
  64. }
  65. if err != nil {
  66. return ""
  67. }
  68. return string(b)
  69. }
  70. func base64Encode(encode int) *base64.Encoding {
  71. switch encode {
  72. case Base64Std:
  73. return base64.StdEncoding
  74. case Base64Url:
  75. return base64.URLEncoding
  76. case Base64RawStd:
  77. return base64.RawStdEncoding
  78. case Base64RawUrl:
  79. return base64.RawURLEncoding
  80. default:
  81. return nil
  82. }
  83. }
  84. func JudgeBase64(str string) bool {
  85. pattern := "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"
  86. matched, err := regexp.MatchString(pattern, str)
  87. if err != nil {
  88. return false
  89. }
  90. if !(len(str)%4 == 0 && matched) {
  91. return false
  92. }
  93. unCodeStr, err := base64.StdEncoding.DecodeString(str)
  94. if err != nil {
  95. return false
  96. }
  97. tranStr := base64.StdEncoding.EncodeToString(unCodeStr)
  98. //return str==base64.StdEncoding.EncodeToString(unCodeStr)
  99. if str == tranStr {
  100. return true
  101. }
  102. return false
  103. }