Skip to content

Commit 0a3f997

Browse files
committed
重构
1 parent a003ac4 commit 0a3f997

File tree

4 files changed

+227
-224
lines changed

4 files changed

+227
-224
lines changed

_examples/des.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"bytes"
99
"fmt"
1010

11-
"github.com/FishGoddess/cryptox"
11+
"github.com/FishGoddess/cryptox/bytes/encoding"
12+
"github.com/FishGoddess/cryptox/bytes/padding"
1213
"github.com/FishGoddess/cryptox/des"
1314
)
1415

@@ -21,26 +22,48 @@ func main() {
2122
msg := []byte("你好,世界")
2223
fmt.Printf("msg: %s\n", msg)
2324

24-
// We use ctr mode and no padding to encrypt data.
25-
// Of course, you can choose another mode if you want.
26-
// Also, you can choose no/zero/pkcs5/pkcs7 to padding data.
27-
encrypted, err := des.EncryptCTR(key, iv, cryptox.PaddingNone, msg)
25+
// For example, we can use ctr mode and no padding to encrypt data.
26+
encrypt, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.None)
2827
if err != nil {
2928
panic(err)
3029
}
3130

32-
fmt.Println("encrypted:", encrypted)
33-
fmt.Println("encrypted hex:", encrypted.Hex())
34-
fmt.Println("encrypted base64:", encrypted.Base64())
31+
// Use encoding to output hex bytes.
32+
encryptHex, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.Hex)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
// Use encoding to output base64 bytes.
38+
encryptBase64, err := des.EncryptCTR(msg, key, iv, padding.PaddingNone, encoding.Base64)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
fmt.Printf("encrypt: %+v\n", encrypt)
44+
fmt.Printf("encrypt hex: %s", encryptHex)
45+
fmt.Printf("encrypt base64: %s", encryptBase64)
3546

3647
// We use ctr mode and no padding to decrypt data.
37-
// Of course, you can choose another mode if you want.
38-
// Also, you can choose no/zero/pkcs5/pkcs7 to undo padding data.
39-
decrypted, err := des.DecryptCTR(key, iv, cryptox.PaddingNone, encrypted)
48+
decrypt, err := des.DecryptCTR(encrypt, key, iv, padding.PaddingNone, encoding.None)
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
// Use encoding to input hex bytes.
54+
decryptHex, err := des.DecryptCTR(encryptHex, key, iv, padding.PaddingNone, encoding.Hex)
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
// Use encoding to input base64 bytes..
60+
decryptBase64, err := des.DecryptCTR(encryptBase64, key, iv, padding.PaddingNone, encoding.Base64)
4061
if err != nil {
4162
panic(err)
4263
}
4364

44-
fmt.Printf("decrypted: %s\n", decrypted)
45-
fmt.Println("decrypted == msg", bytes.Equal(decrypted, msg))
65+
fmt.Printf("decrypt: %s\n", decrypt)
66+
fmt.Printf("decrypt hex: %s\n", decryptHex)
67+
fmt.Printf("decrypt base64: %s\n", decryptBase64)
68+
fmt.Println("decrypt is right: %+v", bytes.Equal(decrypt, msg))
4669
}

des/des_test.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"slices"
1111
"testing"
1212

13-
"github.com/FishGoddess/cryptox"
1413
"github.com/FishGoddess/cryptox/bytes/encoding"
1514
"github.com/FishGoddess/cryptox/bytes/padding"
1615
)
@@ -94,28 +93,6 @@ func testEncryptAndDecrypt(name string, encrypt testEncryptFunc, decrypt testDec
9493
return nil
9594
}
9695

97-
type testResult struct {
98-
bs []byte
99-
hexString string
100-
base64String string
101-
}
102-
103-
func (tr *testResult) compareTo(bs cryptox.Bytes) error {
104-
if string(tr.bs) != string(bs) {
105-
return fmt.Errorf("result bs %s != bs %s", tr.bs, bs)
106-
}
107-
108-
if tr.hexString != bs.Hex() {
109-
return fmt.Errorf("result hexString %s != bs hex %s", tr.hexString, bs.Hex())
110-
}
111-
112-
if tr.base64String != bs.Base64() {
113-
return fmt.Errorf("result base64String %s != bs base64 %s", tr.base64String, bs.Base64())
114-
}
115-
116-
return nil
117-
}
118-
11996
// go test -v -cover -run=^TestNewBlock$
12097
func TestNewBlock(t *testing.T) {
12198
block, blockSize, err := newBlock(testKey)

des/triple_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 newTripleBlock(key cryptox.Bytes) (cipher.Block, int, error) {
17+
func newTripleBlock(key []byte) (cipher.Block, int, error) {
1618
block, err := des.NewTripleDESCipher(key)
1719
if err != nil {
1820
return nil, 0, err
@@ -22,15 +24,15 @@ func newTripleBlock(key cryptox.Bytes) (cipher.Block, int, error) {
2224
return block, blockSize, nil
2325
}
2426

25-
func EncryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
27+
func EncryptTripleECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
2628
block, blockSize, err := newTripleBlock(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 triple len(src) %d %% blockSize %d != 0", len(src), blockSize)
@@ -46,17 +48,22 @@ func EncryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Byt
4648
end += blockSize
4749
}
4850

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

52-
func DecryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
55+
func DecryptTripleECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
5356
block, blockSize, err := newTripleBlock(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 triple len(src) %d %% blockSize %d != 0", len(src), blockSize)
@@ -72,113 +79,133 @@ func DecryptECBTriple(key cryptox.Bytes, padding cryptox.Padding, bs cryptox.Byt
7279
end += blockSize
7380
}
7481

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

78-
func EncryptCBCTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
85+
func EncryptTripleCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
7986
block, blockSize, err := newTripleBlock(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 DecryptCBCTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
100+
func DecryptTripleCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
93101
block, blockSize, err := newTripleBlock(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 EncryptCFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
117+
func EncryptTripleCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
106118
block, blockSize, err := newTripleBlock(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 DecryptCFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
132+
func DecryptTripleCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
120133
block, blockSize, err := newTripleBlock(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 EncryptOFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
149+
func EncryptTripleOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
133150
block, blockSize, err := newTripleBlock(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 DecryptOFBTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
164+
func DecryptTripleOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
147165
block, blockSize, err := newTripleBlock(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 EncryptCTRTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
181+
func EncryptTripleCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
160182
block, blockSize, err := newTripleBlock(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 DecryptCTRTriple(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs cryptox.Bytes) (cryptox.Bytes, error) {
196+
func DecryptTripleCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
174197
block, blockSize, err := newTripleBlock(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)