Skip to content

Commit 0ac4fa3

Browse files
authored
[type:feat] commit base32x.go and base32x_test.go (#73)
1 parent b5fe8f2 commit 0ac4fa3

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

gox/encodingx/base32x/base32x.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022, OpeningO
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package base32x
19+
20+
import "encoding/base32"
21+
22+
// Base32EncodeToString base32x convert bytes to string
23+
func Base32EncodeToString(src []byte) string {
24+
return base32.StdEncoding.EncodeToString(src)
25+
}
26+
27+
// Base32DecodeToBytes base32x convert string to bytes
28+
func Base32DecodeToBytes(src string) ([]byte, error) {
29+
return base32.StdEncoding.DecodeString(src)
30+
}
31+
32+
// Base32HexEncodeToString base32x hex convert bytes to string
33+
func Base32HexEncodeToString(src []byte) string {
34+
return base32.HexEncoding.EncodeToString(src)
35+
}
36+
37+
// Base32HexDecodeToBytes base32x hex convert string to bytes
38+
func Base32HexDecodeToBytes(src string) ([]byte, error) {
39+
return base32.HexEncoding.DecodeString(src)
40+
}

gox/encodingx/base32x/base32x_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2022, OpeningO
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package base32x
19+
20+
import "testing"
21+
22+
// base32x encode to string test cases
23+
func TestBase32EncodeToString(t *testing.T) {
24+
tests := []struct {
25+
msg []byte
26+
want string
27+
}{
28+
{
29+
msg: []byte("I am moremind"),
30+
want: "JEQGC3JANVXXEZLNNFXGI===",
31+
},
32+
{
33+
msg: []byte("hello, OpeningO"),
34+
want: "NBSWY3DPFQQE64DFNZUW4Z2P",
35+
},
36+
}
37+
for _, tt := range tests {
38+
t.Run(string(tt.msg), func(t *testing.T) {
39+
result := Base32EncodeToString(tt.msg)
40+
if result != tt.want {
41+
t.Errorf("Base32EncodeToString() result = %v, want %v", result, tt.want)
42+
}
43+
})
44+
}
45+
46+
}
47+
48+
// base32x decode string to bytes
49+
func TestBase32DecodeToBytes(t *testing.T) {
50+
tests := []struct {
51+
msg string
52+
want []byte
53+
wantErr bool
54+
}{
55+
{
56+
msg: "JEQGC3JANVXXEZLNNFXGI===",
57+
want: []byte("I am moremind"),
58+
wantErr: false,
59+
},
60+
{
61+
msg: "NBSWY3DPFQQE64DFNZUW4Z2P",
62+
want: []byte("hello, OpeningO"),
63+
wantErr: false,
64+
},
65+
}
66+
for _, tt := range tests {
67+
t.Run(tt.msg, func(t *testing.T) {
68+
result, err := Base32DecodeToBytes(tt.msg)
69+
if (err != nil) != tt.wantErr {
70+
t.Errorf("Base32DecodeToBytes() error = %v, wantErr %v", err, tt.wantErr)
71+
return
72+
}
73+
if string(result) != string(tt.want) {
74+
t.Errorf("Base32DecodeToBytes() result = %v, want %v", result, tt.want)
75+
}
76+
})
77+
}
78+
}
79+
80+
// base32x hex encode bytes to string
81+
func TestBase32HexEncodeToString(t *testing.T) {
82+
tests := []struct {
83+
msg []byte
84+
want string
85+
}{
86+
{
87+
msg: []byte("I am moremind"),
88+
want: "94G62R90DLNN4PBDD5N68===",
89+
},
90+
{
91+
msg: []byte("hello, OpeningO"),
92+
want: "D1IMOR3F5GG4US35DPKMSPQF",
93+
},
94+
}
95+
for _, tt := range tests {
96+
t.Run(string(tt.msg), func(t *testing.T) {
97+
result := Base32HexEncodeToString(tt.msg)
98+
if result != tt.want {
99+
t.Errorf("Base32HexEncodeToString() result = %v, want %v", result, tt.want)
100+
}
101+
})
102+
}
103+
}
104+
105+
// base32x hex decode test cases
106+
func TestBase32HexDecodeToBytes(t *testing.T) {
107+
tests := []struct {
108+
msg string
109+
want []byte
110+
wantErr bool
111+
}{
112+
{
113+
msg: "94G62R90DLNN4PBDD5N68===",
114+
want: []byte("I am moremind"),
115+
wantErr: false,
116+
},
117+
{
118+
msg: "D1IMOR3F5GG4US35DPKMSPQF",
119+
want: []byte("hello, OpeningO"),
120+
wantErr: false,
121+
},
122+
}
123+
for _, tt := range tests {
124+
t.Run(tt.msg, func(t *testing.T) {
125+
result, err := Base32HexDecodeToBytes(tt.msg)
126+
if (err != nil) != tt.wantErr {
127+
t.Errorf("Base32HexDecodeToBytes() error = %v, wantErr %v", err, tt.wantErr)
128+
return
129+
}
130+
if string(result) != string(tt.want) {
131+
t.Errorf("Base32HexDecodeToBytes() result = %v, want %v", result, tt.want)
132+
}
133+
})
134+
}
135+
}

0 commit comments

Comments
 (0)