Skip to content

Commit a003ac4

Browse files
committed
调整单测框架
1 parent 23743ae commit a003ac4

File tree

11 files changed

+468
-300
lines changed

11 files changed

+468
-300
lines changed

README.en.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ _Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about mor
2828
$ go get -u github.com/FishGoddess/cryptox
2929
```
3030

31-
* [rand](_examples/rand.go)
3231
* [hash](_examples/hash.go)
3332
* [hmac](_examples/hmac.go)
3433
* [des](_examples/des.go)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ _历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新
2828
$ go get -u github.com/FishGoddess/cryptox
2929
```
3030

31-
* [rand](_examples/rand.go)
3231
* [hash](_examples/hash.go)
3332
* [hmac](_examples/hmac.go)
3433
* [des](_examples/des.go)

bytes/rand/rand.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,30 @@
55
package rand
66

77
import (
8-
"math/rand"
9-
"time"
8+
"math/rand/v2"
109
"unsafe"
1110
)
1211

13-
const words = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
12+
var words = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
1413

15-
var source = rand.NewSource(time.Now().UnixNano())
16-
17-
// AppendBytes appends n random bytes to bs.
18-
func AppendBytes(bs []byte, n int) []byte {
19-
length := int64(len(words))
14+
func appendBytes(bs []byte, n int) []byte {
15+
length := len(words)
2016
for i := 0; i < n; i++ {
21-
index := source.Int63() % length
17+
index := rand.IntN(length)
2218
bs = append(bs, words[index])
2319
}
2420

2521
return bs
2622
}
2723

28-
// Bytes generates n random bytes, and it usually used to generate an iv.
29-
// It means each encrypted data has an different iv so your encrypted data is safer than using a fixed iv.
30-
// However, you should know that the encrypted data of same data will be different if the iv is different.
24+
// Bytes returns n bytes in random which can be used to generate an iv.
3125
func Bytes(n int) []byte {
3226
bs := make([]byte, 0, n)
33-
bs = AppendBytes(bs, n)
27+
bs = appendBytes(bs, n)
3428
return bs
3529
}
3630

37-
// String generates a string including n random bytes.
31+
// String returns a string including n bytes in random which can be used to generate an iv.
3832
func String(n int) string {
3933
bs := Bytes(n)
4034
ptr := unsafe.SliceData(bs)

bytes/rand/rand_test.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,20 @@
55
package rand
66

77
import (
8+
"bytes"
89
"testing"
910
)
1011

11-
// go test -v -cover -run=^TestAppendBytes$
12-
func TestAppendBytes(t *testing.T) {
13-
n := 32
14-
15-
for i := 0; i < 10; i++ {
16-
bs := make([]byte, 0, n)
17-
bs = AppendBytes(bs, n)
18-
19-
if len(bs) != n {
20-
t.Fatalf("len(bs) %d != n %d", len(bs), n)
21-
}
22-
23-
t.Logf("%s\n", bs)
24-
}
25-
}
26-
2712
// go test -v -cover -run=^TestBytes$
2813
func TestBytes(t *testing.T) {
2914
for i := 1; i <= 64; i++ {
3015
bs := Bytes(i)
3116

32-
if len(bs) != i {
33-
t.Fatalf("len(bs) %d != %d", len(bs), i)
17+
for _, b := range bs {
18+
index := bytes.IndexByte(words, b)
19+
if index < 0 {
20+
t.Fatalf("b %+v not in words %+v", b, words)
21+
}
3422
}
3523

3624
t.Logf("%s\n", bs)
@@ -42,8 +30,11 @@ func TestString(t *testing.T) {
4230
for i := 1; i <= 64; i++ {
4331
str := String(i)
4432

45-
if len(str) != i {
46-
t.Fatalf("len(str) %d != %d", len(str), i)
33+
for _, r := range str {
34+
index := bytes.IndexRune(words, r)
35+
if index < 0 {
36+
t.Fatalf("b %+v not in words %+v", r, words)
37+
}
4738
}
4839

4940
t.Logf("%s\n", str)

des/des.go

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
package des
66

77
import (
8+
"bytes"
89
"crypto/cipher"
910
"crypto/des"
1011
"fmt"
1112

12-
"github.com/FishGoddess/cryptox"
13+
"github.com/FishGoddess/cryptox/bytes/encoding"
14+
"github.com/FishGoddess/cryptox/bytes/padding"
1315
)
1416

15-
func newBlock(key cryptox.Bytes) (cipher.Block, int, error) {
17+
func newBlock(key []byte) (cipher.Block, int, error) {
1618
block, err := des.NewCipher(key)
1719
if err != nil {
1820
return nil, 0, err
@@ -22,15 +24,15 @@ func newBlock(key cryptox.Bytes) (cipher.Block, int, error) {
2224
return block, blockSize, nil
2325
}
2426

25-
func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
27+
func EncryptECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
2628
block, blockSize, err := newBlock(key)
2729
if err != nil {
2830
return nil, err
2931
}
3032

31-
bs = bs.Clone()
32-
src := padding.Padding(bs, blockSize)
33-
dst := src.Clone()
33+
src := bytes.Clone(bs)
34+
src = padding.Pad(src, blockSize)
35+
dst := bytes.Clone(src)
3436

3537
if len(src)%blockSize != 0 {
3638
return nil, fmt.Errorf("cryptox/des: encrypt ecb len(src) %d %% blockSize %d != 0", len(src), blockSize)
@@ -46,17 +48,22 @@ func EncryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
4648
end += blockSize
4749
}
4850

51+
dst = encoding.Encode(dst)
4952
return dst, nil
5053
}
5154

52-
func DecryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
55+
func DecryptECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
5356
block, blockSize, err := newBlock(key)
5457
if err != nil {
5558
return nil, err
5659
}
5760

58-
src := bs
59-
dst := bs.Clone()
61+
src, err := encoding.Decode(bs)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
dst := bytes.Clone(src)
6067

6168
if len(src)%blockSize != 0 {
6269
return nil, fmt.Errorf("cryptox/des: decrypt ecb len(src) %d %% blockSize %d != 0", len(src), blockSize)
@@ -72,113 +79,133 @@ func DecryptECB(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (c
7279
end += blockSize
7380
}
7481

75-
return padding.UndoPadding(dst, blockSize)
82+
return padding.Unpad(dst, blockSize)
7683
}
7784

78-
func EncryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
85+
func EncryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
7986
block, blockSize, err := newBlock(key)
8087
if err != nil {
8188
return nil, err
8289
}
8390

84-
bs = bs.Clone()
85-
src := padding.Padding(bs, blockSize)
86-
dst := src.Clone()
91+
src := bytes.Clone(bs)
92+
src = padding.Pad(src, blockSize)
93+
dst := bytes.Clone(src)
8794

8895
cipher.NewCBCEncrypter(block, iv).CryptBlocks(dst, src)
96+
dst = encoding.Encode(dst)
8997
return dst, nil
9098
}
9199

92-
func DecryptCBC(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
100+
func DecryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
93101
block, blockSize, err := newBlock(key)
94102
if err != nil {
95103
return nil, err
96104
}
97105

98-
src := bs
99-
dst := src.Clone()
106+
src, err := encoding.Decode(bs)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
dst := bytes.Clone(src)
100112

101113
cipher.NewCBCDecrypter(block, iv).CryptBlocks(dst, src)
102-
return padding.UndoPadding(dst, blockSize)
114+
return padding.Unpad(dst, blockSize)
103115
}
104116

105-
func EncryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
117+
func EncryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
106118
block, blockSize, err := newBlock(key)
107119
if err != nil {
108120
return nil, err
109121
}
110122

111-
bs = bs.Clone()
112-
src := padding.Padding(bs, blockSize)
113-
dst := src.Clone()
123+
src := bytes.Clone(bs)
124+
src = padding.Pad(src, blockSize)
125+
dst := bytes.Clone(src)
114126

115127
cipher.NewCFBEncrypter(block, iv).XORKeyStream(dst, src)
128+
dst = encoding.Encode(dst)
116129
return dst, nil
117130
}
118131

119-
func DecryptCFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
132+
func DecryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
120133
block, blockSize, err := newBlock(key)
121134
if err != nil {
122135
return nil, err
123136
}
124137

125-
src := bs
126-
dst := bs.Clone()
138+
src, err := encoding.Decode(bs)
139+
if err != nil {
140+
return nil, err
141+
}
142+
143+
dst := bytes.Clone(src)
127144

128145
cipher.NewCFBDecrypter(block, iv).XORKeyStream(dst, src)
129-
return padding.UndoPadding(dst, blockSize)
146+
return padding.Unpad(dst, blockSize)
130147
}
131148

132-
func EncryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
149+
func EncryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
133150
block, blockSize, err := newBlock(key)
134151
if err != nil {
135152
return nil, err
136153
}
137154

138-
bs = bs.Clone()
139-
src := padding.Padding(bs, blockSize)
140-
dst := src.Clone()
155+
src := bytes.Clone(bs)
156+
src = padding.Pad(src, blockSize)
157+
dst := bytes.Clone(src)
141158

142159
cipher.NewOFB(block, iv).XORKeyStream(dst, src)
160+
dst = encoding.Encode(dst)
143161
return dst, nil
144162
}
145163

146-
func DecryptOFB(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
164+
func DecryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
147165
block, blockSize, err := newBlock(key)
148166
if err != nil {
149167
return nil, err
150168
}
151169

152-
src := bs
153-
dst := bs.Clone()
170+
src, err := encoding.Decode(bs)
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
dst := bytes.Clone(src)
154176

155177
cipher.NewOFB(block, iv).XORKeyStream(dst, src)
156-
return padding.UndoPadding(dst, blockSize)
178+
return padding.Unpad(dst, blockSize)
157179
}
158180

159-
func EncryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
181+
func EncryptCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
160182
block, blockSize, err := newBlock(key)
161183
if err != nil {
162184
return nil, err
163185
}
164186

165-
bs = bs.Clone()
166-
src := padding.Padding(bs, blockSize)
167-
dst := src.Clone()
187+
src := bytes.Clone(bs)
188+
src = padding.Pad(src, blockSize)
189+
dst := bytes.Clone(src)
168190

169191
cipher.NewCTR(block, iv).XORKeyStream(dst, src)
192+
dst = encoding.Encode(dst)
170193
return dst, nil
171194
}
172195

173-
func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
196+
func DecryptCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
174197
block, blockSize, err := newBlock(key)
175198
if err != nil {
176199
return nil, err
177200
}
178201

179-
src := bs
180-
dst := bs.Clone()
202+
src, err := encoding.Decode(bs)
203+
if err != nil {
204+
return nil, err
205+
}
206+
207+
dst := bytes.Clone(src)
181208

182209
cipher.NewCTR(block, iv).XORKeyStream(dst, src)
183-
return padding.UndoPadding(dst, blockSize)
210+
return padding.Unpad(dst, blockSize)
184211
}

0 commit comments

Comments
 (0)