gbase64.go 2.9 KB

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