/
base64.go
101 lines (82 loc) · 2.03 KB
/
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
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
package processors
import (
"encoding/base64"
"fmt"
)
var base64RawFlag = Flag{
Name: "raw",
Short: "r",
Desc: "unpadded base64 encoding",
Value: false,
Type: FlagBool,
}
func checkBase64RawFlag(f []Flag) bool {
raw := false
for _, flag := range f {
if flag.Name == "raw" || flag.Short == "r" {
r, ok := flag.Value.(bool)
if ok {
raw = r
}
}
}
return raw
}
// Base64Encode encode string to base64.
type Base64Encode struct{}
func (p Base64Encode) Name() string {
return "base64-encode"
}
func (p Base64Encode) Alias() []string {
return []string{"b64-enc", "b64-encode"}
}
func (p Base64Encode) Transform(data []byte, f ...Flag) (string, error) {
if checkBase64RawFlag(f) {
return base64.RawStdEncoding.EncodeToString(data), nil
}
return base64.StdEncoding.EncodeToString(data), nil
}
func (p Base64Encode) Flags() []Flag {
return []Flag{base64RawFlag}
}
func (p Base64Encode) Title() string {
title := "Base64 Encoding"
return fmt.Sprintf("%s (%s)", title, p.Name())
}
func (p Base64Encode) Description() string {
return "Encode your text to Base64"
}
func (p Base64Encode) FilterValue() string {
return p.Title()
}
// Base64Decode decode string from base64 to plain text.
type Base64Decode struct{}
func (p Base64Decode) Name() string {
return "base64-decode"
}
func (p Base64Decode) Alias() []string {
return []string{"b64-dec", "b64-decode"}
}
func (p Base64Decode) Transform(data []byte, f ...Flag) (string, error) {
var decodedString []byte
var err error
if checkBase64RawFlag(f) {
decodedString, err = base64.RawStdEncoding.DecodeString(string(data))
} else {
decodedString, err = base64.StdEncoding.DecodeString(string(data))
}
return string(decodedString), err
}
func (p Base64Decode) Flags() []Flag {
return []Flag{base64RawFlag}
}
func (p Base64Decode) Title() string {
title := "Base64 Decode"
return fmt.Sprintf("%s (%s)", title, p.Name())
}
func (p Base64Decode) Description() string {
return "Decode your base64 text"
}
func (p Base64Decode) FilterValue() string {
return p.Title()
}