-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
90 lines (67 loc) · 2.38 KB
/
encoder.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
package generic
import (
"encoding/base32"
"encoding/base64"
"encoding/hex"
)
type Encoder interface {
Encode([]byte) string
Decode(string) ([]byte, error)
}
// StdEncoding is the standard base64 encoding, as defined in RFC 4648.
type Base64 struct{}
// URLEncoding is the alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names.
type UrlBase64 struct{}
// nolint: lll
// RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names. This is the same as URLEncoding but omits padding characters.
type RawUrlBase64 struct{}
// nolint: lll
// RawStdEncoding is the standard raw, unpadded base64 encoding, as defined in RFC 4648 section 3.2. This is the same as StdEncoding but omits padding characters.
type RawBase64 struct{}
type Base32 struct{}
type PaddinglessBase32 struct{}
type Hex struct{}
func (b *Base64) Encode(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
func (b *Base64) Decode(data string) ([]byte, error) {
return base64.StdEncoding.DecodeString(data)
}
func (b *UrlBase64) Encode(data []byte) string {
return base64.URLEncoding.EncodeToString(data)
}
func (b *UrlBase64) Decode(data string) ([]byte, error) {
return base64.URLEncoding.DecodeString(data)
}
func (b *RawUrlBase64) Encode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
func (b *RawUrlBase64) Decode(data string) ([]byte, error) {
return base64.RawURLEncoding.DecodeString(data)
}
func (b *RawBase64) Encode(data []byte) string {
return base64.RawStdEncoding.EncodeToString(data)
}
func (b *RawBase64) Decode(data string) ([]byte, error) {
return base64.RawStdEncoding.DecodeString(data)
}
func (b *Base32) Encode(data []byte) string {
return base32.StdEncoding.EncodeToString(data)
}
func (b *Base32) Decode(data string) ([]byte, error) {
return base32.StdEncoding.DecodeString(data)
}
func (b *PaddinglessBase32) Encode(data []byte) string {
encoder := base32.StdEncoding.WithPadding(base32.NoPadding)
return encoder.EncodeToString(data)
}
func (b *PaddinglessBase32) Decode(data string) ([]byte, error) {
encoder := base32.StdEncoding.WithPadding(base32.NoPadding)
return encoder.DecodeString(data)
}
func (h *Hex) Encode(data []byte) string {
return hex.EncodeToString(data)
}
func (h *Hex) Decode(data string) ([]byte, error) {
return hex.DecodeString(data)
}