gbase64.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "github.com/gogf/gf/util/gconv"
  11. "io/ioutil"
  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 gconv.UnsafeBytesToStr(Encode(src))
  26. }
  27. // EncryptFile 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. return nil, err
  32. }
  33. return Encode(content), nil
  34. }
  35. // MustEncodeFile encodes file content of <path> using BASE64 algorithms.
  36. // It panics if any error occurs.
  37. func MustEncodeFile(path string) []byte {
  38. result, err := EncodeFile(path)
  39. if err != nil {
  40. panic(err)
  41. }
  42. return result
  43. }
  44. // EncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
  45. func EncodeFileToString(path string) (string, error) {
  46. content, err := EncodeFile(path)
  47. if err != nil {
  48. return "", err
  49. }
  50. return gconv.UnsafeBytesToStr(content), nil
  51. }
  52. // MustEncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
  53. // It panics if any error occurs.
  54. func MustEncodeFileToString(path string) string {
  55. result, err := EncodeFileToString(path)
  56. if err != nil {
  57. panic(err)
  58. }
  59. return result
  60. }
  61. // Decode decodes bytes with BASE64 algorithm.
  62. func Decode(data []byte) ([]byte, error) {
  63. src := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
  64. n, err := base64.StdEncoding.Decode(src, data)
  65. return src[:n], err
  66. }
  67. // MustDecode decodes bytes with BASE64 algorithm.
  68. // It panics if any error occurs.
  69. func MustDecode(data []byte) []byte {
  70. result, err := Decode(data)
  71. if err != nil {
  72. panic(err)
  73. }
  74. return result
  75. }
  76. // DecodeString decodes string with BASE64 algorithm.
  77. func DecodeString(data string) ([]byte, error) {
  78. return Decode([]byte(data))
  79. }
  80. // MustDecodeString decodes string with BASE64 algorithm.
  81. // It panics if any error occurs.
  82. func MustDecodeString(data string) []byte {
  83. result, err := DecodeString(data)
  84. if err != nil {
  85. panic(err)
  86. }
  87. return result
  88. }
  89. // DecodeString decodes string with BASE64 algorithm.
  90. func DecodeToString(data string) (string, error) {
  91. b, err := DecodeString(data)
  92. return gconv.UnsafeBytesToStr(b), err
  93. }
  94. // MustDecodeToString decodes string with BASE64 algorithm.
  95. // It panics if any error occurs.
  96. func MustDecodeToString(data string) string {
  97. result, err := DecodeToString(data)
  98. if err != nil {
  99. panic(err)
  100. }
  101. return result
  102. }