Skip to content

Commit 131b5e2

Browse files
committed
更新
1 parent 361703f commit 131b5e2

File tree

3 files changed

+198
-21
lines changed

3 files changed

+198
-21
lines changed

tool/encoding/encoding.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,87 @@ import (
44
"bytes"
55
"strings"
66
"encoding/hex"
7+
"encoding/base32"
78
"encoding/base64"
89
)
910

10-
var (
11-
defaultEncoding = New()
12-
)
11+
// StdEncoding
12+
var StdEncoding = New()
1313

1414
/**
15-
* 编码
15+
* Encoding
1616
*
1717
* @create 2022-4-17
1818
* @author deatil
1919
*/
2020
type Encoding struct{}
2121

22-
// 构造函数
22+
// New Encoding
2323
func New() *Encoding {
2424
return &Encoding{}
2525
}
2626

27-
// Base64 编码
27+
// Base32 Encode
28+
func (this *Encoding) Base32Encode(src []byte) string {
29+
return base32.StdEncoding.EncodeToString(src)
30+
}
31+
32+
// Base32 Encode
33+
func Base32Encode(src []byte) string {
34+
return StdEncoding.Base32Encode(src)
35+
}
36+
37+
// Base32 Decode
38+
func (this *Encoding) Base32Decode(s string) ([]byte, error) {
39+
return base32.StdEncoding.DecodeString(s)
40+
}
41+
42+
// Base32 Decode
43+
func Base32Decode(s string) ([]byte, error) {
44+
return StdEncoding.Base32Decode(s)
45+
}
46+
47+
// Base64 Encode
2848
func (this *Encoding) Base64Encode(src []byte) string {
2949
return base64.StdEncoding.EncodeToString(src)
3050
}
3151

52+
// Base64 Encode
3253
func Base64Encode(src []byte) string {
33-
return defaultEncoding.Base64Encode(src)
54+
return StdEncoding.Base64Encode(src)
3455
}
3556

36-
// Base64 解码
57+
// Base64 Decode
3758
func (this *Encoding) Base64Decode(s string) ([]byte, error) {
3859
return base64.StdEncoding.DecodeString(s)
3960
}
4061

62+
// Base64 Decode
4163
func Base64Decode(s string) ([]byte, error) {
42-
return defaultEncoding.Base64Decode(s)
64+
return StdEncoding.Base64Decode(s)
4365
}
4466

45-
// Hex 编码
67+
// Hex Encode
4668
func (this *Encoding) HexEncode(src []byte) string {
4769
return hex.EncodeToString(src)
4870
}
4971

72+
// Hex Encode
5073
func HexEncode(src []byte) string {
51-
return defaultEncoding.HexEncode(src)
74+
return StdEncoding.HexEncode(src)
5275
}
5376

54-
// Hex 解码
77+
// Hex Decode
5578
func (this *Encoding) HexDecode(s string) ([]byte, error) {
5679
return hex.DecodeString(s)
5780
}
5881

82+
// Hex Decode
5983
func HexDecode(s string) ([]byte, error) {
60-
return defaultEncoding.HexDecode(s)
84+
return StdEncoding.HexDecode(s)
6185
}
6286

63-
// 补码
87+
// Hex Padding
6488
func (this *Encoding) HexPadding(text string, size int) string {
6589
if size < 1 {
6690
return text
@@ -79,11 +103,12 @@ func (this *Encoding) HexPadding(text string, size int) string {
79103
return text[n-size:]
80104
}
81105

106+
// Hex Padding
82107
func HexPadding(text string, size int) string {
83-
return defaultEncoding.HexPadding(text, size)
108+
return StdEncoding.HexPadding(text, size)
84109
}
85110

86-
// BytesPadding
111+
// Bytes Padding
87112
func (this *Encoding) BytesPadding(text []byte, size int) []byte {
88113
if size < 1 {
89114
return text
@@ -103,6 +128,7 @@ func (this *Encoding) BytesPadding(text []byte, size int) []byte {
103128
return text[n-size:]
104129
}
105130

131+
// Bytes Padding
106132
func BytesPadding(text []byte, size int) []byte {
107-
return defaultEncoding.BytesPadding(text, size)
133+
return StdEncoding.BytesPadding(text, size)
108134
}

tool/encoding/encoding_test.go

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,21 @@ func Test_HexPadding(t *testing.T) {
3838
}
3939

4040
for _, c := range cases {
41-
res := HexPadding(c.src, c.size)
41+
{
42+
res := HexPadding(c.src, c.size)
43+
44+
assertEqual(res, c.check, "Test_HexPadding")
45+
}
46+
{
47+
res := New().HexPadding(c.src, c.size)
4248

43-
assertEqual(res, c.check, "Test_HexPadding")
49+
assertEqual(res, c.check, "Test_HexPadding")
50+
}
51+
{
52+
res := StdEncoding.HexPadding(c.src, c.size)
53+
54+
assertEqual(res, c.check, "Test_HexPadding")
55+
}
4456
}
4557
}
4658

@@ -75,8 +87,146 @@ func Test_BytesPadding(t *testing.T) {
7587
}
7688

7789
for i, c := range cases {
78-
res := BytesPadding(c.src, c.size)
90+
{
91+
res := BytesPadding(c.src, c.size)
92+
93+
assertEqual(res, c.check, fmt.Sprintf("#%d: Test_BytesPadding", i))
94+
}
95+
{
96+
res := New().BytesPadding(c.src, c.size)
97+
98+
assertEqual(res, c.check, fmt.Sprintf("#%d: Test_BytesPadding", i))
99+
}
100+
{
101+
res := StdEncoding.BytesPadding(c.src, c.size)
102+
103+
assertEqual(res, c.check, fmt.Sprintf("#%d: Test_BytesPadding", i))
104+
}
105+
}
106+
}
107+
108+
func Test_Base32Encode(t *testing.T) {
109+
for _, p := range base32Pairs {
110+
got := Base32Encode([]byte(p.decoded))
111+
test.Equalf(t, p.encoded, got, "Base32Encode(%q) = %q, want %q", p.decoded, got, p.encoded)
112+
}
113+
}
114+
115+
func Test_Base32Decode(t *testing.T) {
116+
for _, p := range base32Pairs {
117+
got, err := Base32Decode(p.encoded)
118+
119+
test.NoErrorf(t, err, "Base32Decode(%q) = error %v, want %v", p.encoded, err, error(nil))
120+
test.Equalf(t, p.decoded, string(got), "Base32Decode(%q) = %q, want %q", p.encoded, string(got), p.decoded)
121+
}
122+
}
123+
124+
func Test_Base64Encode(t *testing.T) {
125+
for _, p := range base64Pairs {
126+
got := Base64Encode([]byte(p.decoded))
127+
test.Equalf(t, p.encoded, got, "Base64Encode(%q) = %q, want %q", p.decoded, got, p.encoded)
128+
}
129+
}
79130

80-
assertEqual(res, c.check, fmt.Sprintf("#%d: Test_BytesPadding", i))
131+
func Test_Base64Decode(t *testing.T) {
132+
for _, p := range base64Pairs {
133+
got, err := Base64Decode(p.encoded)
134+
135+
test.NoErrorf(t, err, "Base64Decode(%q) = error %v, want %v", p.encoded, err, error(nil))
136+
test.Equalf(t, p.decoded, string(got), "Base64Decode(%q) = %q, want %q", p.encoded, string(got), p.decoded)
137+
}
138+
}
139+
140+
func Test_HexEncode(t *testing.T) {
141+
for _, p := range hexEncDecTests {
142+
got := HexEncode(p.dec)
143+
test.Equalf(t, p.enc, got, "HexEncode(%q) = %q, want %q", p.dec, got, p.enc)
144+
}
145+
}
146+
147+
func Test_HexDecode(t *testing.T) {
148+
for _, p := range hexEncDecTests {
149+
got, err := HexDecode(p.enc)
150+
151+
test.NoErrorf(t, err, "HexDecode(%q) = error %v, want %v", p.enc, err, error(nil))
152+
test.Equalf(t, p.dec, got, "HexDecode(%q) = %q, want %q", p.enc, got, p.dec)
81153
}
82154
}
155+
156+
type testpair struct {
157+
decoded, encoded string
158+
}
159+
160+
var base32Pairs = []testpair{
161+
// RFC 4648 examples
162+
{"", ""},
163+
{"f", "MY======"},
164+
{"fo", "MZXQ===="},
165+
{"foo", "MZXW6==="},
166+
{"foob", "MZXW6YQ="},
167+
{"fooba", "MZXW6YTB"},
168+
{"foobar", "MZXW6YTBOI======"},
169+
170+
// Wikipedia examples, converted to base32
171+
{"sure.", "ON2XEZJO"},
172+
{"sure", "ON2XEZI="},
173+
{"sur", "ON2XE==="},
174+
{"su", "ON2Q===="},
175+
{"leasure.", "NRSWC43VOJSS4==="},
176+
{"easure.", "MVQXG5LSMUXA===="},
177+
{"asure.", "MFZXK4TFFY======"},
178+
{"sure.", "ON2XEZJO"},
179+
180+
// bigtest
181+
{
182+
"Twas brillig, and the slithy toves",
183+
"KR3WC4ZAMJZGS3DMNFTSYIDBNZSCA5DIMUQHG3DJORUHSIDUN53GK4Y=",
184+
},
185+
}
186+
187+
var base64Pairs = []testpair{
188+
// RFC 3548 examples
189+
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
190+
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
191+
{"\x14\xfb\x9c\x03", "FPucAw=="},
192+
193+
// RFC 4648 examples
194+
{"", ""},
195+
{"f", "Zg=="},
196+
{"fo", "Zm8="},
197+
{"foo", "Zm9v"},
198+
{"foob", "Zm9vYg=="},
199+
{"fooba", "Zm9vYmE="},
200+
{"foobar", "Zm9vYmFy"},
201+
202+
// Wikipedia examples
203+
{"sure.", "c3VyZS4="},
204+
{"sure", "c3VyZQ=="},
205+
{"sur", "c3Vy"},
206+
{"su", "c3U="},
207+
{"leasure.", "bGVhc3VyZS4="},
208+
{"easure.", "ZWFzdXJlLg=="},
209+
{"asure.", "YXN1cmUu"},
210+
{"sure.", "c3VyZS4="},
211+
212+
// bigtest
213+
{
214+
"Twas brillig, and the slithy toves",
215+
"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
216+
},
217+
}
218+
219+
type encDecTest struct {
220+
enc string
221+
dec []byte
222+
}
223+
224+
var hexEncDecTests = []encDecTest{
225+
{"", []byte{}},
226+
{"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}},
227+
{"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}},
228+
{"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}},
229+
{"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}},
230+
{"67", []byte{'g'}},
231+
{"e3a1", []byte{0xe3, 0xa1}},
232+
}

tool/test/format.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func NoErrorf(t TestingT, err error, msg string, args ...any) bool {
217217

218218
return NoError(t, err, append([]any{msg}, args...)...)
219219
}
220+
220221
// NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the
221222
// specified substring or element.
222223
//

0 commit comments

Comments
 (0)