-
Notifications
You must be signed in to change notification settings - Fork 68
/
encrypter.go
72 lines (61 loc) · 1.8 KB
/
encrypter.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
package dongle
// Encrypter defines Encrypter struct.
// 定义 Encrypter 结构体
type Encrypter struct {
dongle
}
// newEncrypter returns a new Encrypter instance.
// 初始化 Encrypter 结构体
func newEncrypter() Encrypter {
return Encrypter{}
}
// FromString encrypts from string.
// 对字符串加密
func (e Encrypter) FromString(s string) Encrypter {
e.src = string2bytes(s)
return e
}
// FromBytes encrypts from byte slice.
// 对字节切片加密
func (e Encrypter) FromBytes(b []byte) Encrypter {
e.src = b
return e
}
// String implements Stringer interface for Encrypter struct.
// 实现 Stringer 接口
func (e Encrypter) String() string {
return e.ToRawString()
}
// ToRawString outputs as raw string without encoding.
// 输出未经编码的原始字符串
func (e Encrypter) ToRawString() string {
return bytes2string(e.dst)
}
// ToHexString outputs as string with hex encoding.
// 输出经过 hex 编码的字符串
func (e Encrypter) ToHexString() string {
return Encode.FromBytes(e.dst).ByHex().ToString()
}
// ToBase64String outputs as string with base64 encoding.
// 输出经过 base64 编码的字符串
func (e Encrypter) ToBase64String() string {
return Encode.FromBytes(e.dst).ByBase64().ToString()
}
// ToRawBytes outputs as raw byte slice without encoding.
// 输出未经编码的原始字节切片
func (e Encrypter) ToRawBytes() []byte {
if len(e.dst) == 0 {
return []byte("")
}
return e.dst
}
// ToHexBytes outputs as byte with hex encoding.
// 输出经过 hex 编码的字节切片
func (e Encrypter) ToHexBytes() []byte {
return Encode.FromBytes(e.dst).ByHex().ToBytes()
}
// ToBase64Bytes outputs as byte slice with base64 encoding.
// 输出经过 base64 编码的字节切片
func (e Encrypter) ToBase64Bytes() []byte {
return Encode.FromBytes(e.dst).ByBase64().ToBytes()
}