-
Notifications
You must be signed in to change notification settings - Fork 69
/
baseX.go
285 lines (261 loc) · 5.98 KB
/
baseX.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package dongle
import (
"encoding/ascii85"
"encoding/base32"
"encoding/base64"
"encoding/hex"
"github.com/dromara/dongle/base100"
"github.com/dromara/dongle/base45"
"github.com/dromara/dongle/base58"
"github.com/dromara/dongle/base62"
"github.com/dromara/dongle/base91"
)
// ByHex encodes by hex.
func (e Encoder) ByHex() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, hex.EncodedLen(len(e.src)))
hex.Encode(buf, e.src)
e.dst = buf
return e
}
// ByHex decodes by hex.
func (d Decoder) ByHex() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, hex.DecodedLen(len(d.src)))
n, err := hex.Decode(buf, d.src)
if n > 0 {
d.dst = buf
}
if err != nil {
d.Error = NewDecodeError().ModeError("hex")
return d
}
return d
}
// ByBase16 encodes by base16.
func (e Encoder) ByBase16() Encoder {
return e.ByHex()
}
// ByBase16 decodes by base16.
func (d Decoder) ByBase16() Decoder {
if d.ByHex().Error != nil {
d.Error = NewDecodeError().ModeError("base16")
return d
}
return d.ByHex()
}
// ByBase32 encodes by base32.
func (e Encoder) ByBase32() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, base32.StdEncoding.EncodedLen(len(e.src)))
base32.StdEncoding.Encode(buf, e.src)
e.dst = buf
return e
}
// ByBase32 decodes by base32.
func (d Decoder) ByBase32() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, base32.StdEncoding.DecodedLen(len(d.src)))
n, err := base32.StdEncoding.Decode(buf, d.src)
if err != nil {
d.Error = NewDecodeError().ModeError("base32")
return d
}
d.dst, d.Error = buf[:n], err
return d
}
// ByBase45 encodes by base45.
func (e Encoder) ByBase45() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
dst := base45.Encode(bytes2string(e.src))
e.dst = string2bytes(dst)
return e
}
// ByBase45 decodes by base45.
func (d Decoder) ByBase45() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
dst, err := base45.Decode(bytes2string(d.src))
d.dst, d.Error = string2bytes(dst), err
return d
}
// ByBase58 encodes by base58.
func (e Encoder) ByBase58() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
e.dst = base58.Encode(e.src)
return e
}
// ByBase58 decodes by base58.
func (d Decoder) ByBase58() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
d.dst = base58.Decode(d.src)
return d
}
// ByBase62 encodes by base62.
func (e Encoder) ByBase62(alphabet ...string) Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
table := base62.StdAlphabet
if len(alphabet) > 0 {
table = alphabet[0]
}
enc := base62.NewEncoding(table)
if enc.Error != nil {
e.Error = enc.Error
return e
}
e.dst = base62.NewEncoding(table).Encode(e.src)
return e
}
// ByBase62 decodes by base62.
func (d Decoder) ByBase62(alphabet ...string) Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
table := base62.StdAlphabet
if len(alphabet) > 0 {
table = alphabet[0]
}
enc := base62.NewEncoding(table)
if enc.Error != nil {
d.Error = enc.Error
return d
}
d.dst, d.Error = base62.NewEncoding(table).Decode(d.src)
if d.Error != nil {
d.Error = NewDecodeError().ModeError("base62")
return d
}
return d
}
// ByBase64 encodes by base64.
func (e Encoder) ByBase64() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, base64.StdEncoding.EncodedLen(len(e.src)))
base64.StdEncoding.Encode(buf, e.src)
e.dst = buf
return e
}
// ByBase64 decodes by base64.
func (d Decoder) ByBase64() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, base64.StdEncoding.DecodedLen(len(d.src)))
n, err := base64.StdEncoding.Decode(buf, d.src)
if err != nil {
d.Error = NewDecodeError().ModeError("base64")
return d
}
d.dst = buf[:n]
return d
}
// ByBase64URL encodes by base64 for url.
func (e Encoder) ByBase64URL() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, base64.URLEncoding.EncodedLen(len(e.src)))
base64.URLEncoding.Encode(buf, e.src)
e.dst = buf
return e
}
// ByBase64URL decodes by base64 for url.
func (d Decoder) ByBase64URL() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, base64.URLEncoding.DecodedLen(len(d.src)))
n, err := base64.URLEncoding.Decode(buf, d.src)
if err != nil {
d.Error = NewDecodeError().ModeError("base64URL")
return d
}
d.dst, d.Error = buf[:n], err
return d
}
// ByBase85 encodes by base85.
func (e Encoder) ByBase85() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, ascii85.MaxEncodedLen(len(e.src)))
n := ascii85.Encode(buf, e.src)
e.dst = buf[:n]
return e
}
// ByBase85 decodes by base85.
func (d Decoder) ByBase85() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, len(d.src))
n, _, err := ascii85.Decode(buf, d.src, true)
if err != nil {
d.Error = NewDecodeError().ModeError("base85")
return d
}
d.dst = buf[:n]
return d
}
// ByBase91 encodes by base91.
func (e Encoder) ByBase91() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
buf := make([]byte, base91.StdEncoding.EncodedLen(len(e.src)))
n := base91.StdEncoding.Encode(buf, e.src)
e.dst = buf[:n]
return e
}
// ByBase91 decodes by base91.
func (d Decoder) ByBase91() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
buf := make([]byte, base91.StdEncoding.DecodedLen(len(d.src)))
n, err := base91.StdEncoding.Decode(buf, d.src)
if err != nil {
d.Error = NewDecodeError().ModeError("base91")
return d
}
d.dst = buf[:n]
return d
}
// ByBase100 encodes by base100.
func (e Encoder) ByBase100() Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
e.dst = base100.Encode(e.src)
return e
}
// ByBase100 decodes by base100.
func (d Decoder) ByBase100() Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
d.dst, d.Error = base100.Decode(d.src)
if d.Error != nil {
d.Error = NewDecodeError().ModeError("base100")
}
return d
}