github.com/wangyougui/gf/v2@v2.6.5/encoding/gbase64/gbase64.go (about) 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/wangyougui/gf. 6 7 // Package gbase64 provides useful API for BASE64 encoding/decoding algorithm. 8 package gbase64 9 10 import ( 11 "encoding/base64" 12 "os" 13 14 "github.com/wangyougui/gf/v2/errors/gerror" 15 ) 16 17 // Encode encodes bytes with BASE64 algorithm. 18 func Encode(src []byte) []byte { 19 dst := make([]byte, base64.StdEncoding.EncodedLen(len(src))) 20 base64.StdEncoding.Encode(dst, src) 21 return dst 22 } 23 24 // EncodeString encodes string with BASE64 algorithm. 25 func EncodeString(src string) string { 26 return EncodeToString([]byte(src)) 27 } 28 29 // EncodeToString encodes bytes to string with BASE64 algorithm. 30 func EncodeToString(src []byte) string { 31 return string(Encode(src)) 32 } 33 34 // EncodeFile encodes file content of `path` using BASE64 algorithms. 35 func EncodeFile(path string) ([]byte, error) { 36 content, err := os.ReadFile(path) 37 if err != nil { 38 err = gerror.Wrapf(err, `os.ReadFile failed for filename "%s"`, path) 39 return nil, err 40 } 41 return Encode(content), nil 42 } 43 44 // MustEncodeFile encodes file content of `path` using BASE64 algorithms. 45 // It panics if any error occurs. 46 func MustEncodeFile(path string) []byte { 47 result, err := EncodeFile(path) 48 if err != nil { 49 panic(err) 50 } 51 return result 52 } 53 54 // EncodeFileToString encodes file content of `path` to string using BASE64 algorithms. 55 func EncodeFileToString(path string) (string, error) { 56 content, err := EncodeFile(path) 57 if err != nil { 58 return "", err 59 } 60 return string(content), nil 61 } 62 63 // MustEncodeFileToString encodes file content of `path` to string using BASE64 algorithms. 64 // It panics if any error occurs. 65 func MustEncodeFileToString(path string) string { 66 result, err := EncodeFileToString(path) 67 if err != nil { 68 panic(err) 69 } 70 return result 71 } 72 73 // Decode decodes bytes with BASE64 algorithm. 74 func Decode(data []byte) ([]byte, error) { 75 var ( 76 src = make([]byte, base64.StdEncoding.DecodedLen(len(data))) 77 n, err = base64.StdEncoding.Decode(src, data) 78 ) 79 if err != nil { 80 err = gerror.Wrap(err, `base64.StdEncoding.Decode failed`) 81 } 82 return src[:n], err 83 } 84 85 // MustDecode decodes bytes with BASE64 algorithm. 86 // It panics if any error occurs. 87 func MustDecode(data []byte) []byte { 88 result, err := Decode(data) 89 if err != nil { 90 panic(err) 91 } 92 return result 93 } 94 95 // DecodeString decodes string with BASE64 algorithm. 96 func DecodeString(data string) ([]byte, error) { 97 return Decode([]byte(data)) 98 } 99 100 // MustDecodeString decodes string with BASE64 algorithm. 101 // It panics if any error occurs. 102 func MustDecodeString(data string) []byte { 103 result, err := DecodeString(data) 104 if err != nil { 105 panic(err) 106 } 107 return result 108 } 109 110 // DecodeToString decodes string with BASE64 algorithm. 111 func DecodeToString(data string) (string, error) { 112 b, err := DecodeString(data) 113 return string(b), err 114 } 115 116 // MustDecodeToString decodes string with BASE64 algorithm. 117 // It panics if any error occurs. 118 func MustDecodeToString(data string) string { 119 result, err := DecodeToString(data) 120 if err != nil { 121 panic(err) 122 } 123 return result 124 }