Skip to content

Commit ed882e8

Browse files
committed
增加 encoding 包
1 parent ad6d000 commit ed882e8

File tree

9 files changed

+188
-3
lines changed

9 files changed

+188
-3
lines changed

internal/encoding/base64.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import "encoding/base64"
8+
9+
type encodingBase64 struct{}
10+
11+
func (encodingBase64) Encode(bs []byte) []byte {
12+
enc := base64.StdEncoding
13+
n := enc.EncodedLen(len(bs))
14+
buffer := make([]byte, n)
15+
enc.Encode(buffer, bs)
16+
17+
return buffer[:n]
18+
}
19+
20+
func (encodingBase64) Decode(bs []byte) ([]byte, error) {
21+
enc := base64.StdEncoding
22+
n := enc.DecodedLen(len(bs))
23+
buffer := make([]byte, n)
24+
25+
n, err := enc.Decode(buffer, bs)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return buffer[:n], nil
31+
}

internal/encoding/base64_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestBase64$
10+
func TestBase64(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, EncodingData: []byte{}},
13+
{Data: []byte("Hello World"), EncodingData: []byte("SGVsbG8gV29ybGQ=")},
14+
{Data: []byte("你好,世界"), EncodingData: []byte("5L2g5aW977yM5LiW55WM")},
15+
}
16+
17+
testEncoding(t, Base64, testCases)
18+
}

internal/encoding/encoding.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
var (
8+
None Encoding = encodingNone{}
9+
Hex Encoding = encodingHex{}
10+
Base64 Encoding = encodingBase64{}
11+
)
12+
13+
// Encoding encodes a byte slice to another byte slice in some way and decodes it from the byte slice.
14+
type Encoding interface {
15+
// Encode encodes the byte slice.
16+
Encode(bs []byte) []byte
17+
18+
// Decode decodes the byte slice.
19+
Decode(bs []byte) ([]byte, error)
20+
}

internal/encoding/encoding_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import (
8+
"slices"
9+
"testing"
10+
)
11+
12+
type testCase struct {
13+
Data []byte
14+
EncodingData []byte
15+
}
16+
17+
func testEncoding(t *testing.T, encoding Encoding, testCases []testCase) {
18+
for _, testCase := range testCases {
19+
got := encoding.Encode(testCase.Data)
20+
want := testCase.EncodingData
21+
22+
if !slices.Equal(got, want) {
23+
t.Fatalf("got %+v != want %+v", got, want)
24+
}
25+
26+
got, err := encoding.Decode(got)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
want = testCase.Data
32+
if !slices.Equal(got, want) {
33+
t.Fatalf("got %+v != want %+v", got, want)
34+
}
35+
}
36+
}

internal/encoding/hex.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import "encoding/hex"
8+
9+
type encodingHex struct{}
10+
11+
func (encodingHex) Encode(bs []byte) []byte {
12+
n := hex.EncodedLen(len(bs))
13+
buffer := make([]byte, n)
14+
15+
n = hex.Encode(buffer, bs)
16+
return buffer[:n]
17+
}
18+
19+
func (encodingHex) Decode(bs []byte) ([]byte, error) {
20+
n := hex.DecodedLen(len(bs))
21+
buffer := make([]byte, n)
22+
23+
n, err := hex.Decode(buffer, bs)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
return buffer[:n], nil
29+
}

internal/encoding/hex_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestHex$
10+
func TestHex(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, EncodingData: []byte{}},
13+
{Data: []byte("Hello World"), EncodingData: []byte("48656c6c6f20576f726c64")},
14+
{Data: []byte("你好,世界"), EncodingData: []byte("e4bda0e5a5bdefbc8ce4b896e7958c")},
15+
}
16+
17+
testEncoding(t, Hex, testCases)
18+
}

internal/encoding/none.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
type encodingNone struct{}
8+
9+
func (encodingNone) Encode(bs []byte) []byte {
10+
return bs
11+
}
12+
13+
func (encodingNone) Decode(bs []byte) ([]byte, error) {
14+
return bs, nil
15+
}

internal/encoding/none_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package encoding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestNone$
10+
func TestNone(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, EncodingData: []byte{}},
13+
{Data: []byte("Hello World"), EncodingData: []byte("Hello World")},
14+
{Data: []byte("你好,世界"), EncodingData: []byte("你好,世界")},
15+
}
16+
17+
testEncoding(t, None, testCases)
18+
}

internal/padding/padding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ var (
1111
PKCS7 Padding = paddingPKCS7{}
1212
)
1313

14-
// Padding will pad a byte slice with some []byte and unpad them with the same []byte.
14+
// Padding will pad a byte slice with some bytes and unpad them with the same bytes.
1515
type Padding interface {
16-
// Pad pads some []byte to the byte slice.
16+
// Pad pads some bytes to the byte slice.
1717
Pad(bs []byte, blockSize int) []byte
1818

19-
// Unpad unpads the []byte from the byte slice.
19+
// Unpad unpads the bytes from the byte slice.
2020
Unpad(bs []byte, blockSize int) ([]byte, error)
2121
}

0 commit comments

Comments
 (0)