/
code.go
192 lines (175 loc) · 4.47 KB
/
code.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package utils
import (
"bytes"
"encoding/base32"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"net/url"
"regexp"
"strconv"
"strings"
)
// 只接受string和[]byte类型
func stringAndByte(s interface{}) []byte {
var byte_s []byte
var err error
switch s.(type) {
case string:
byte_s = []byte(s.(string))
case []byte:
byte_s = s.([]byte)
default:
err = errors.New("Please check whether the type is string and []byte.")
panic(err)
}
return byte_s
}
// Hex编码
func HexEncode(s interface{}) []byte {
byte_s := stringAndByte(s)
dst := make([]byte, hex.EncodedLen(len(byte_s)))
n := hex.Encode(dst, byte_s)
return dst[:n]
}
// Hex解码
func HexDecode(s interface{}) []byte {
byte_s := stringAndByte(s)
dst := make([]byte, hex.DecodedLen(len(byte_s)))
n, err := hex.Decode(dst, byte_s)
if err != nil {
panic(err)
}
return dst[:n]
}
// URI编码
func EncodeURIComponent(s interface{}) string {
byte_s := stringAndByte(s)
es := url.QueryEscape(string(byte_s))
es = strings.ReplaceAll(es, "+", "%20")
es = strings.ReplaceAll(es, "%21", "!")
es = strings.ReplaceAll(es, "%27", "'")
es = strings.ReplaceAll(es, "%28", "(")
es = strings.ReplaceAll(es, "%29", ")")
es = strings.ReplaceAll(es, "%2A", "*")
return es
}
// URI解码
func DecodeURIComponent(s interface{}) string {
byte_s := stringAndByte(s)
t, err := url.QueryUnescape(strings.ReplaceAll(strings.ReplaceAll(string(byte_s), "+", "%2B"), "%20", "+"))
if err != nil {
panic(err)
}
return t
}
// URI编码
func EncodeURI(s interface{}) string {
byte_s := stringAndByte(s)
es := EncodeURIComponent(string(byte_s))
ss := "!#$&'()*+,-./:=?@_~"
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "%"+strings.ToUpper(string(HexEncode(string(ss[i])))), string(ss[i]))
}
return strings.ReplaceAll(es, "%3B", ";")
}
// URI解码
func DecodeURI(s interface{}) string {
byte_s := stringAndByte(s)
es := string(byte_s)
ss := "!#$&'()*+,-./:=?@_~"
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "%"+strings.ToUpper(string(HexEncode(string(ss[i])))), "$"+"%"+strings.ToUpper(string(HexEncode(string(ss[i]))))+"$")
}
es = DecodeURIComponent(es)
for i := 0; i < len(ss); i++ {
es = strings.ReplaceAll(es, "$"+string(ss[i])+"$", "%"+strings.ToUpper(string(HexEncode(string(ss[i])))))
}
return es
}
// Base32编码
func Base32Encode(s interface{}) string {
byte_s := stringAndByte(s)
return base32.StdEncoding.EncodeToString(byte_s)
}
// Base32解码
func Base32Decode(s interface{}) string {
byte_s := stringAndByte(s)
str, err := base32.StdEncoding.DecodeString(string(byte_s))
if err != nil {
panic(err)
}
return string(str)
}
// Base64编码
func Btoa(s interface{}) string {
byte_s := stringAndByte(s)
return base64.StdEncoding.EncodeToString(byte_s)
}
// Base64编码,同上
func Base64Encode(s interface{}) string {
byte_s := stringAndByte(s)
return Btoa(byte_s)
}
// Base64解码
func Atob(s interface{}) string {
byte_s := stringAndByte(s)
str, err := base64.StdEncoding.DecodeString(string(byte_s))
if err != nil {
panic(err)
}
return string(str)
}
// Base64解码,同上
func Base64Decode(s interface{}) string {
byte_s := stringAndByte(s)
return Atob(byte_s)
}
// 中文转Unicode
func Escape(s interface{}) string {
byte_s := stringAndByte(s)
str := string(byte_s)
es := ""
for _, s := range str {
switch {
case s >= '0' && s <= '9':
es += string(s)
case s >= 'a' && s <= 'z':
es += string(s)
case s >= 'A' && s <= 'Z':
es += string(s)
case strings.Contains("*+-./@_", string(s)):
es += string(s)
case int(s) <= 127:
es += "%" + strings.ToUpper(string(HexEncode(string(s))))
case int(s) >= 128:
es += strings.ReplaceAll(strings.ReplaceAll(strconv.QuoteToASCII(string(s)), "\"", ""), "\\u", "%u")
}
}
return es
}
// Unicode转中文
func UnEscape(s interface{}) string {
byte_s := stringAndByte(s)
str := string(byte_s)
re, _ := regexp.Compile("(%u)[0-9a-zA-Z]{4}")
str = re.ReplaceAllStringFunc(str, func(st string) string {
bs, _ := hex.DecodeString(strings.ReplaceAll(st, "%u", ""))
r := uint16(0)
binary.Read(bytes.NewReader(bs), binary.BigEndian, &r)
return string(r)
})
str = DecodeURIComponent(str)
return str
}
// Marshal 避免json.Marshal对 "<", ">", "&" 等字符进行HTML编码
func Marshal(data interface{}) ([]byte, error) {
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
// 禁用HTML转义
encoder.SetEscapeHTML(false)
err := encoder.Encode(data)
return buffer.Bytes(), err
}