forked from dromara/dongle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
57 lines (50 loc) · 994 Bytes
/
encode.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
package dongle
const (
// BASE32 base32 encoding mode
BASE32 = "base32"
// BASE58 base58 encoding mode
BASE58 = "base58"
// BASE64 base64 encoding mode
BASE64 = "base64"
// HEX hex encoding mode
HEX = "hex"
)
// encode defines a encode struct.
type encode struct {
dongle
}
// newEncode returns a new encode instance.
func newEncode() encode {
return encode{}
}
// FromString encrypts from string.
func (e encode) FromString(s string) encode {
if s == emptyString {
return e
}
e.input = string2bytes(s)
return e
}
// FromBytes encrypts from byte slice.
func (e encode) FromBytes(b []byte) encode {
if len(b) > 0 {
e.input = b
}
return e
}
// ToString outputs as string.
func (e encode) ToString() string {
output := e.output
if output == nil {
output = e.input
}
return bytes2string(output)
}
// ToBytes outputs as byte slice.
func (e encode) ToBytes() []byte {
input, output := e.input, e.output
if len(input) == 0 {
return emptyBytes
}
return output
}