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