Skip to content

Commit e10d981

Browse files
committed
完善注释
1 parent a94d4d9 commit e10d981

File tree

19 files changed

+49
-233
lines changed

19 files changed

+49
-233
lines changed

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $ go get -u github.com/FishGoddess/cryptox
4242
$ make bench
4343
```
4444

45-
_Note: Data size is 128 bytes, ecb/cbc uses pkcs7 padding, cfb/ofb/ctr is no padding._
45+
_Note: Data size is 128 bytes, ecb/cbc uses pkcs7 padding, cfb/ofb/ctr/gcm is no padding._
4646

4747
```
4848
goos: linux

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $ go get -u github.com/FishGoddess/cryptox
4242
$ make bench
4343
```
4444

45-
_注:数据为 128 字节,ecb/cbc 为 pkcs7 填充,cfb/ofb/ctr 为不填充。_
45+
_注:数据为 128 字节,ecb/cbc 为 pkcs7 填充,cfb/ofb/ctr/gcm 为不填充。_
4646

4747
```
4848
goos: linux

_examples/rsa.key

Lines changed: 0 additions & 51 deletions
This file was deleted.

_examples/rsa.pub

Lines changed: 0 additions & 14 deletions
This file was deleted.

aes/aes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func newBlock(key []byte) (cipher.Block, int, error) {
2424
return block, blockSize, nil
2525
}
2626

27+
// EncryptECB uses ecb mode to encrypt bs.
2728
func EncryptECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
2829
block, blockSize, err := newBlock(key)
2930
if err != nil {
@@ -52,6 +53,7 @@ func EncryptECB(bs []byte, key []byte, padding padding.Padding, encoding encodin
5253
return dst, nil
5354
}
5455

56+
// DecryptECB uses ecb mode to decrypt bs.
5557
func DecryptECB(bs []byte, key []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
5658
block, blockSize, err := newBlock(key)
5759
if err != nil {
@@ -82,6 +84,7 @@ func DecryptECB(bs []byte, key []byte, padding padding.Padding, encoding encodin
8284
return padding.Unpad(dst, blockSize)
8385
}
8486

87+
// EncryptCBC uses cbc mode to encrypt bs.
8588
func EncryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
8689
block, blockSize, err := newBlock(key)
8790
if err != nil {
@@ -97,6 +100,7 @@ func EncryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
97100
return dst, nil
98101
}
99102

103+
// DecryptCBC uses cbc mode to decrypt bs.
100104
func DecryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
101105
block, blockSize, err := newBlock(key)
102106
if err != nil {
@@ -114,6 +118,7 @@ func DecryptCBC(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
114118
return padding.Unpad(dst, blockSize)
115119
}
116120

121+
// EncryptCFB uses cfb mode to encrypt bs.
117122
func EncryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
118123
block, blockSize, err := newBlock(key)
119124
if err != nil {
@@ -129,6 +134,7 @@ func EncryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
129134
return dst, nil
130135
}
131136

137+
// DecryptCFB uses cfb mode to decrypt bs.
132138
func DecryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
133139
block, blockSize, err := newBlock(key)
134140
if err != nil {
@@ -146,6 +152,7 @@ func DecryptCFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
146152
return padding.Unpad(dst, blockSize)
147153
}
148154

155+
// EncryptOFB uses ofb mode to encrypt bs.
149156
func EncryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
150157
block, blockSize, err := newBlock(key)
151158
if err != nil {
@@ -161,6 +168,7 @@ func EncryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
161168
return dst, nil
162169
}
163170

171+
// DecryptOFB uses ofb mode to decrypt bs.
164172
func DecryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
165173
block, blockSize, err := newBlock(key)
166174
if err != nil {
@@ -178,6 +186,7 @@ func DecryptOFB(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
178186
return padding.Unpad(dst, blockSize)
179187
}
180188

189+
// EncryptCTR uses ctr mode to encrypt bs.
181190
func EncryptCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
182191
block, blockSize, err := newBlock(key)
183192
if err != nil {
@@ -193,6 +202,7 @@ func EncryptCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encod
193202
return dst, nil
194203
}
195204

205+
// DecryptCTR uses ctr mode to decrypt bs.
196206
func DecryptCTR(bs []byte, key []byte, iv []byte, padding padding.Padding, encoding encoding.Encoding) ([]byte, error) {
197207
block, blockSize, err := newBlock(key)
198208
if err != nil {

bytes.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,17 @@ package cryptox
66

77
import (
88
"encoding/base64"
9-
"encoding/hex"
109
"io"
1110
"os"
1211
)
1312

1413
type Bytes []byte
1514

16-
// ParseHex a hex string to bytes.
17-
func ParseHex(hexString string) (Bytes, error) {
18-
return hex.DecodeString(hexString)
19-
}
20-
21-
// ParseBase64 a base64 string to bytes.
22-
func ParseBase64(base64String string) (Bytes, error) {
23-
return base64.StdEncoding.DecodeString(base64String)
24-
}
25-
26-
// Hex returns Bytes as hex.
27-
func (bs Bytes) Hex() string {
28-
return hex.EncodeToString(bs)
29-
}
30-
3115
// Base64 returns Bytes as base64.
3216
func (bs Bytes) Base64() string {
3317
return base64.StdEncoding.EncodeToString(bs)
3418
}
3519

36-
// Clone clones bs and returns a new slice.
37-
func (bs Bytes) Clone() Bytes {
38-
newSlice := make([]byte, len(bs))
39-
copy(newSlice, bs)
40-
41-
return newSlice
42-
}
43-
4420
func (bs Bytes) newFile(path string) (*os.File, error) {
4521
flag := os.O_CREATE | os.O_EXCL | os.O_WRONLY
4622
mode := os.FileMode(0644)

bytes/encoding/base64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "encoding/base64"
88

99
type encodingBase64 struct{}
1010

11+
// Encode encodes the byte slice with base64 encoding.
1112
func (encodingBase64) Encode(bs []byte) []byte {
1213
enc := base64.StdEncoding
1314
n := enc.EncodedLen(len(bs))
@@ -17,6 +18,7 @@ func (encodingBase64) Encode(bs []byte) []byte {
1718
return buffer[:n]
1819
}
1920

21+
// Decode decodes the byte slice with base64 encoding.
2022
func (encodingBase64) Decode(bs []byte) ([]byte, error) {
2123
enc := base64.StdEncoding
2224
n := enc.DecodedLen(len(bs))

bytes/encoding/encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var (
1010
Base64 Encoding = encodingBase64{}
1111
)
1212

13-
// Encoding encodes a byte slice to another byte slice in some way and decodes it from the byte slice.
13+
// Encoding transfers a byte slice to another byte slice in some way.
1414
type Encoding interface {
1515
// Encode encodes the byte slice.
1616
Encode(bs []byte) []byte

bytes/encoding/hex.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "encoding/hex"
88

99
type encodingHex struct{}
1010

11+
// Encode encodes the byte slice with hex encoding.
1112
func (encodingHex) Encode(bs []byte) []byte {
1213
n := hex.EncodedLen(len(bs))
1314
buffer := make([]byte, n)
@@ -16,6 +17,7 @@ func (encodingHex) Encode(bs []byte) []byte {
1617
return buffer[:n]
1718
}
1819

20+
// Decode decodes the byte slice with hex encoding.
1921
func (encodingHex) Decode(bs []byte) ([]byte, error) {
2022
n := hex.DecodedLen(len(bs))
2123
buffer := make([]byte, n)

bytes/encoding/none.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ package encoding
66

77
type encodingNone struct{}
88

9+
// Encode returns the original byte slice.
910
func (encodingNone) Encode(bs []byte) []byte {
1011
return bs
1112
}
1213

14+
// Decode returns the original byte slice.
1315
func (encodingNone) Decode(bs []byte) ([]byte, error) {
1416
return bs, nil
1517
}

0 commit comments

Comments
 (0)