gbase64.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gbase64 provides useful API for BASE64 encoding/decoding algorithm.
  7. package gbase64
  8. import (
  9. "encoding/base64"
  10. "io/ioutil"
  11. "github.com/gogf/gf/v2/errors/gerror"
  12. )
  13. // Encode encodes bytes with BASE64 algorithm.
  14. func Encode(src []byte) []byte {
  15. dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
  16. base64.StdEncoding.Encode(dst, src)
  17. return dst
  18. }
  19. // EncodeString encodes string with BASE64 algorithm.
  20. func EncodeString(src string) string {
  21. return EncodeToString([]byte(src))
  22. }
  23. // EncodeToString encodes bytes to string with BASE64 algorithm.
  24. func EncodeToString(src []byte) string {
  25. return string(Encode(src))
  26. }
  27. // EncodeFile encodes file content of `path` using BASE64 algorithms.
  28. func EncodeFile(path string) ([]byte, error) {
  29. content, err := ioutil.ReadFile(path)
  30. if err != nil {
  31. err = gerror.Wrapf(err, `ioutil.ReadFile failed for filename "%s"`, path)
  32. return nil, err
  33. }
  34. return Encode(content), nil
  35. }
  36. // MustEncodeFile encodes file content of `path` using BASE64 algorithms.
  37. // It panics if any error occurs.
  38. func MustEncodeFile(path string) []byte {
  39. result, err := EncodeFile(path)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return result
  44. }
  45. // EncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
  46. func EncodeFileToString(path string) (string, error) {
  47. content, err := EncodeFile(path)
  48. if err != nil {
  49. return "", err
  50. }
  51. return string(content), nil
  52. }
  53. // MustEncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
  54. // It panics if any error occurs.
  55. func MustEncodeFileToString(path string) string {
  56. result, err := EncodeFileToString(path)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return result
  61. }
  62. // Decode decodes bytes with BASE64 algorithm.
  63. func Decode(data []byte) ([]byte, error) {
  64. var (
  65. src = make([]byte, base64.StdEncoding.DecodedLen(len(data)))
  66. n, err = base64.StdEncoding.Decode(src, data)
  67. )
  68. if err != nil {
  69. err = gerror.Wrap(err, `base64.StdEncoding.Decode failed`)
  70. }
  71. return src[:n], err
  72. }
  73. // MustDecode decodes bytes with BASE64 algorithm.
  74. // It panics if any error occurs.
  75. func MustDecode(data []byte) []byte {
  76. result, err := Decode(data)
  77. if err != nil {
  78. panic(err)
  79. }
  80. return result
  81. }
  82. // DecodeString decodes string with BASE64 algorithm.
  83. func DecodeString(data string) ([]byte, error) {
  84. return Decode([]byte(data))
  85. }
  86. // MustDecodeString decodes string with BASE64 algorithm.
  87. // It panics if any error occurs.
  88. func MustDecodeString(data string) []byte {
  89. result, err := DecodeString(data)
  90. if err != nil {
  91. panic(err)
  92. }
  93. return result
  94. }
  95. // DecodeToString decodes string with BASE64 algorithm.
  96. func DecodeToString(data string) (string, error) {
  97. b, err := DecodeString(data)
  98. return string(b), err
  99. }
  100. // MustDecodeToString decodes string with BASE64 algorithm.
  101. // It panics if any error occurs.
  102. func MustDecodeToString(data string) string {
  103. result, err := DecodeToString(data)
  104. if err != nil {
  105. panic(err)
  106. }
  107. return result
  108. }