-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.go
47 lines (36 loc) · 995 Bytes
/
base64.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
package utils
import "encoding/base64"
type StringCoding interface {
Encode(data []byte) string
Decode(data string) ([]byte, error)
}
type Base64StringCoding struct {
}
func (b *Base64StringCoding) Encode(data []byte) string {
return Base64Encode(data)
}
func (b *Base64StringCoding) Decode(data string) ([]byte, error) {
return Base64Decode(data)
}
type WithStringCoder interface {
Coder() StringCoding
}
type WithStringCoderBase struct {
StringCoding StringCoding
}
func (w *WithStringCoderBase) Construct(encoder ...StringCoding) {
if len(encoder) == 0 {
w.StringCoding = &Base64StringCoding{}
} else {
w.StringCoding = encoder[0]
}
}
func (w *WithStringCoderBase) Coder() StringCoding {
return w.StringCoding
}
func Base64Encode(data []byte) string {
return base64.RawStdEncoding.WithPadding(base64.StdPadding).EncodeToString(data)
}
func Base64Decode(data string) ([]byte, error) {
return base64.RawStdEncoding.WithPadding(base64.StdPadding).DecodeString(data)
}