File tree Expand file tree Collapse file tree 9 files changed +188
-3
lines changed
Expand file tree Collapse file tree 9 files changed +188
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 .
1515type 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}
You can’t perform that action at this time.
0 commit comments