Skip to content

Commit ad6d000

Browse files
committed
调整 padding 包
1 parent 40740f3 commit ad6d000

12 files changed

Lines changed: 264 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ on:
77

88
jobs:
99
test-project:
10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-latest
1111
steps:
12+
- run: uname -a
13+
- run: lsb_release -a
14+
15+
- name: Setup
16+
uses: actions/setup-go@v4
17+
with:
18+
go-version: "1.25"
19+
20+
- run: go version
21+
1222
- name: Checkout
13-
uses: actions/checkout@v3
23+
uses: actions/checkout@v4
1424

1525
- name: Test
16-
run: make test
26+
run: make test

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/FishGoddess/cryptox
22

3-
go 1.21
3+
go 1.25

internal/padding/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 padding
6+
7+
type paddingNone struct{}
8+
9+
func (paddingNone) Pad(bs []byte, blockSize int) []byte {
10+
return bs
11+
}
12+
13+
func (paddingNone) Unpad(bs []byte, blockSize int) ([]byte, error) {
14+
return bs, nil
15+
}

internal/padding/none_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 padding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestNone$
10+
func TestNone(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, PaddingData: []byte{}},
13+
{Data: []byte{1, 2, 3, 4, 5}, PaddingData: []byte{1, 2, 3, 4, 5}},
14+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8}},
15+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0}},
16+
}
17+
18+
testPadding(t, None, testCases)
19+
}

internal/padding/padding.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 padding
6+
7+
var (
8+
None Padding = paddingNone{}
9+
Zero Padding = paddingZero{}
10+
PKCS5 Padding = paddingPKCS5{}
11+
PKCS7 Padding = paddingPKCS7{}
12+
)
13+
14+
// Padding will pad a byte slice with some []byte and unpad them with the same []byte.
15+
type Padding interface {
16+
// Pad pads some []byte to the byte slice.
17+
Pad(bs []byte, blockSize int) []byte
18+
19+
// Unpad unpads the []byte from the byte slice.
20+
Unpad(bs []byte, blockSize int) ([]byte, error)
21+
}

internal/padding/padding_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 padding
6+
7+
import (
8+
"slices"
9+
"testing"
10+
)
11+
12+
type testCase struct {
13+
Data []byte
14+
PaddingData []byte
15+
}
16+
17+
func testPadding(t *testing.T, padding Padding, testCases []testCase) {
18+
blockSize := 8
19+
20+
for _, testCase := range testCases {
21+
got := padding.Pad(testCase.Data, blockSize)
22+
want := testCase.PaddingData
23+
24+
if !slices.Equal(got, want) {
25+
t.Fatalf("got %+v != want %+v", got, want)
26+
}
27+
28+
got, err := padding.Unpad(got, blockSize)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
want = testCase.Data
34+
if !slices.Equal(got, want) {
35+
t.Fatalf("got %+v != want %+v", got, want)
36+
}
37+
}
38+
}

internal/padding/pkcs5.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 padding
6+
7+
import "fmt"
8+
9+
type paddingPKCS5 struct{}
10+
11+
func (paddingPKCS5) Pad(bs []byte, blockSize int) []byte {
12+
padding := blockSize - (len(bs) % blockSize)
13+
for i := 0; i < padding; i++ {
14+
bs = append(bs, byte(padding))
15+
}
16+
17+
return bs
18+
}
19+
20+
func (paddingPKCS5) Unpad(bs []byte, blockSize int) ([]byte, error) {
21+
length := len(bs)
22+
number := int(bs[length-1])
23+
24+
if number > length {
25+
return nil, fmt.Errorf("cryptox: unpad number %d > length %d", number, length)
26+
}
27+
28+
if number > blockSize {
29+
return nil, fmt.Errorf("cryptox: unpad number %d > blockSize %d", number, blockSize)
30+
}
31+
32+
return bs[:length-number], nil
33+
}

internal/padding/pkcs5_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 padding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestPKCS5$
10+
func TestPKCS5(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, PaddingData: []byte{8, 8, 8, 8, 8, 8, 8, 8}},
13+
{Data: []byte{1, 2, 3, 4, 5}, PaddingData: []byte{1, 2, 3, 4, 5, 3, 3, 3}},
14+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8}},
15+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}},
16+
}
17+
18+
testPadding(t, PKCS5, testCases)
19+
}

internal/padding/pkcs7.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 padding
6+
7+
import "fmt"
8+
9+
type paddingPKCS7 struct{}
10+
11+
func (paddingPKCS7) Pad(bs []byte, blockSize int) []byte {
12+
padding := blockSize - (len(bs) % blockSize)
13+
for i := 0; i < padding; i++ {
14+
bs = append(bs, byte(padding))
15+
}
16+
17+
return bs
18+
}
19+
20+
func (paddingPKCS7) Unpad(bs []byte, blockSize int) ([]byte, error) {
21+
length := len(bs)
22+
number := int(bs[length-1])
23+
24+
if number > length {
25+
return nil, fmt.Errorf("cryptox: unpad number %d > length %d", number, length)
26+
}
27+
28+
if number > blockSize {
29+
return nil, fmt.Errorf("cryptox: unpad number %d > blockSize %d", number, blockSize)
30+
}
31+
32+
return bs[:length-number], nil
33+
}

internal/padding/pkcs7_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 padding
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestPKCS7$
10+
func TestPKCS7(t *testing.T) {
11+
testCases := []testCase{
12+
{Data: []byte{}, PaddingData: []byte{8, 8, 8, 8, 8, 8, 8, 8}},
13+
{Data: []byte{1, 2, 3, 4, 5}, PaddingData: []byte{1, 2, 3, 4, 5, 3, 3, 3}},
14+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8}},
15+
{Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8}, PaddingData: []byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}},
16+
}
17+
18+
testPadding(t, PKCS7, testCases)
19+
}

0 commit comments

Comments
 (0)