蛋蛋星球-客户端
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.

пре 3 недеља
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package utils
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. )
  6. const (
  7. Base64Std = iota
  8. Base64Url
  9. Base64RawStd
  10. Base64RawUrl
  11. )
  12. func Base64StdEncode(str interface{}) string {
  13. return Base64Encode(str, Base64Std)
  14. }
  15. func Base64StdDecode(str interface{}) string {
  16. return Base64Decode(str, Base64Std)
  17. }
  18. func Base64UrlEncode(str interface{}) string {
  19. return Base64Encode(str, Base64Url)
  20. }
  21. func Base64UrlDecode(str interface{}) string {
  22. return Base64Decode(str, Base64Url)
  23. }
  24. func Base64RawStdEncode(str interface{}) string {
  25. return Base64Encode(str, Base64RawStd)
  26. }
  27. func Base64RawStdDecode(str interface{}) string {
  28. return Base64Decode(str, Base64RawStd)
  29. }
  30. func Base64RawUrlEncode(str interface{}) string {
  31. return Base64Encode(str, Base64RawUrl)
  32. }
  33. func Base64RawUrlDecode(str interface{}) string {
  34. return Base64Decode(str, Base64RawUrl)
  35. }
  36. func Base64Encode(str interface{}, encode int) string {
  37. newEncode := base64Encode(encode)
  38. if newEncode == nil {
  39. return ""
  40. }
  41. switch v := str.(type) {
  42. case string:
  43. return newEncode.EncodeToString([]byte(v))
  44. case []byte:
  45. return newEncode.EncodeToString(v)
  46. }
  47. return newEncode.EncodeToString([]byte(fmt.Sprint(str)))
  48. }
  49. func Base64Decode(str interface{}, encode int) string {
  50. var err error
  51. var b []byte
  52. newEncode := base64Encode(encode)
  53. if newEncode == nil {
  54. return ""
  55. }
  56. switch v := str.(type) {
  57. case string:
  58. b, err = newEncode.DecodeString(v)
  59. case []byte:
  60. b, err = newEncode.DecodeString(string(v))
  61. default:
  62. return ""
  63. }
  64. if err != nil {
  65. return ""
  66. }
  67. return string(b)
  68. }
  69. func base64Encode(encode int) *base64.Encoding {
  70. switch encode {
  71. case Base64Std:
  72. return base64.StdEncoding
  73. case Base64Url:
  74. return base64.URLEncoding
  75. case Base64RawStd:
  76. return base64.RawStdEncoding
  77. case Base64RawUrl:
  78. return base64.RawURLEncoding
  79. default:
  80. return nil
  81. }
  82. }