Skip to content

Commit f1e2121

Browse files
committed
重构 padding 和 encoding 和 rand 包
1 parent ed882e8 commit f1e2121

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+511
-442
lines changed

FUTURE.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
## ✈️ 未来版本的新特性 (Features in future versions)
22

3-
### v0.5.x
3+
### v0.6.x
44

55
* [ ] 增加 cmd 包,提供二进制 cli 使用
66
* [ ] 支持 SM3 国标算法
77
* [ ] 支持 SM4 国标算法
8+
9+
### v0.5.x
10+
811
* [ ] 支持 ECC key 生成
912
* [ ] 支持 ECC 非对称加密算法
1013
* [ ] 支持更多的散列算法
14+
* [ ] 重构代码,优化使用方式
15+
* [ ] 继续完善单元测试,提升覆盖率到 90% 以上
1116

1217
### v0.4.x
1318

1419
* [x] 调整 hash 包使用姿势
1520
* [x] 大规模重构,优化代码使用
1621
* [x] 性能优化,尝试减少内存分配
17-
* [ ] 继续完善单元测试,提升覆盖率到 90%
1822

1923
### v0.3.x
2024

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* DES/3DES/AES encrypt and decrypt supports.
1919
* RSA/ECC encrypt and decrypt supports.
2020
* ECB/CBC/OFB/CFB/CTR/GCM mode supports.
21-
* NONE/ZERO/PKCS5/PKCS7 padding supports.
21+
* ZERO/PKCS5/PKCS7 padding supports.
2222

2323
_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._
2424

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* 支持 DES/3DES/AES 等对称加密算法。
1919
* 支持 RSA/ECC 等非对称加密算法。
2020
* 支持 ECB/CBC/OFB/CFB/CTR/GCM 等分组模式。
21-
* 支持 NONE/ZERO/PKCS5/PKCS7 等填充方式
21+
* 支持 ZERO/PKCS5/PKCS7 等字节填充方式
2222

2323
_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_
2424

_examples/hash.go

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,72 @@ package main
77
import (
88
"fmt"
99

10+
"github.com/FishGoddess/cryptox/bytes/encoding"
1011
"github.com/FishGoddess/cryptox/hash"
1112
)
1213

1314
func main() {
1415
data := []byte("你好,世界")
15-
fmt.Println("data:", data)
16-
17-
// All hashing functions will return a cryptox.Bytes type which can be encoded to hex and base64.
18-
19-
md5 := hash.MD5(data)
20-
fmt.Println("md5 hex:", md5.Hex())
21-
fmt.Println("md5 base64:", md5.Base64())
22-
23-
sha1 := hash.SHA1(data)
24-
fmt.Println("sha1 hex:", sha1.Hex())
25-
fmt.Println("sha1 base64:", sha1.Base64())
26-
27-
sha224 := hash.SHA224(data)
28-
fmt.Println("sha224 hex:", sha224.Hex())
29-
fmt.Println("sha224 base64:", sha224.Base64())
30-
31-
sha256 := hash.SHA256(data)
32-
fmt.Println("sha256 hex:", sha256.Hex())
33-
fmt.Println("sha256 base64:", sha256.Base64())
34-
35-
sha384 := hash.SHA384(data)
36-
fmt.Println("sha384 hex:", sha384.Hex())
37-
fmt.Println("sha384 base64:", sha384.Base64())
38-
39-
sha512 := hash.SHA512(data)
40-
fmt.Println("sha512 hex:", sha512.Hex())
41-
fmt.Println("sha512 base64:", sha512.Base64())
16+
fmt.Printf("data: %s\n", data)
17+
18+
md5Hex := hash.MD5(data, encoding.Hex)
19+
md5Base64 := hash.MD5(data, encoding.Base64)
20+
fmt.Printf("md5 hex: %s\n", md5Hex)
21+
fmt.Printf("md5 base64: %s\n", md5Base64)
22+
23+
sha1Hex := hash.SHA1(data, encoding.Hex)
24+
sha1Base64 := hash.SHA1(data, encoding.Base64)
25+
fmt.Printf("sha1 hex: %s\n", sha1Hex)
26+
fmt.Printf("sha1 base64: %s\n", sha1Base64)
27+
28+
sha224Hex := hash.SHA224(data, encoding.Hex)
29+
sha224Base64 := hash.SHA224(data, encoding.Base64)
30+
fmt.Printf("sha224 hex: %s\n", sha224Hex)
31+
fmt.Printf("sha224 base64: %s\n", sha224Base64)
32+
33+
sha256Hex := hash.SHA256(data, encoding.Hex)
34+
sha256Base64 := hash.SHA256(data, encoding.Base64)
35+
fmt.Printf("sha256 hex: %s\n", sha256Hex)
36+
fmt.Printf("sha256 base64: %s\n", sha256Base64)
37+
38+
sha384Hex := hash.SHA384(data, encoding.Hex)
39+
sha384Base64 := hash.SHA384(data, encoding.Base64)
40+
fmt.Printf("sha384 hex: %s\n", sha384Hex)
41+
fmt.Printf("sha384 base64: %s\n", sha384Base64)
42+
43+
sha512Hex := hash.SHA512(data, encoding.Hex)
44+
sha512Base64 := hash.SHA512(data, encoding.Base64)
45+
fmt.Printf("sha512 hex: %s\n", sha512Hex)
46+
fmt.Printf("sha512 base64: %s\n", sha512Base64)
4247

4348
crc32 := hash.CRC32IEEE(data)
44-
fmt.Printf("crc32 with ieee: %d\n", crc32)
49+
fmt.Printf("crc32 ieee: %d\n", crc32)
4550

4651
crc64 := hash.CRC64ISO(data)
47-
fmt.Printf("crc64 with iso: %d\n", crc64)
52+
fmt.Printf("crc64 iso: %d\n", crc64)
4853

4954
crc64 = hash.CRC64ECMA(data)
50-
fmt.Printf("crc64 with ecma: %d\n", crc64)
55+
fmt.Printf("crc64 ecma: %d\n", crc64)
5156

5257
fnv32 := hash.Fnv32(data)
53-
fmt.Printf("fnv-1/32: %d\n", fnv32)
58+
fmt.Printf("fnv32: %d\n", fnv32)
5459

5560
fnv32a := hash.Fnv32a(data)
56-
fmt.Printf("fnv-1/32a: %d\n", fnv32a)
61+
fmt.Printf("fnv32a: %d\n", fnv32a)
5762

5863
fnv64 := hash.Fnv64(data)
59-
fmt.Printf("fnv-1/64: %d\n", fnv64)
64+
fmt.Printf("fnv64: %d\n", fnv64)
6065

6166
fnv64a := hash.Fnv64a(data)
62-
fmt.Printf("fnv-1/64a: %d\n", fnv64a)
67+
fmt.Printf("fnv64a: %d\n", fnv64a)
6368

64-
fnv128 := hash.Fnv128(data)
65-
fmt.Printf("fnv-1/128 hex: %s\n", fnv128.Hex())
66-
fmt.Printf("fnv-1/128 base64: %s\n", fnv128.Base64())
69+
fnv128Hex := hash.Fnv128(data, encoding.Hex)
70+
fnv128Base64 := hash.Fnv128(data, encoding.Base64)
71+
fmt.Printf("fnv128 hex: %s\n", fnv128Hex)
72+
fmt.Printf("fnv128 base64: %s\n", fnv128Base64)
6773

68-
fnv128a := hash.Fnv128a(data)
69-
fmt.Printf("fnv-1/128a hex: %s\n", fnv128a.Hex())
70-
fmt.Printf("fnv-1/128a base64: %s\n", fnv128a.Base64())
74+
fnv128aHex := hash.Fnv128a(data, encoding.Hex)
75+
fnv128aBase64 := hash.Fnv128a(data, encoding.Base64)
76+
fmt.Printf("fnv128a hex: %s\n", fnv128aHex)
77+
fmt.Printf("fnv128a base64: %s\n", fnv128aBase64)
7178
}

_examples/hash_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"testing"
99

10+
"github.com/FishGoddess/cryptox/bytes/encoding"
1011
"github.com/FishGoddess/cryptox/hash"
1112
)
1213

@@ -20,7 +21,7 @@ func BenchmarkMD5(b *testing.B) {
2021
b.ResetTimer()
2122

2223
for i := 0; i < b.N; i++ {
23-
hash.MD5(benchData)
24+
hash.MD5(benchData, encoding.None)
2425
}
2526
}
2627

@@ -30,7 +31,7 @@ func BenchmarkSHA1(b *testing.B) {
3031
b.ResetTimer()
3132

3233
for i := 0; i < b.N; i++ {
33-
hash.SHA1(benchData)
34+
hash.SHA1(benchData, encoding.None)
3435
}
3536
}
3637

@@ -40,7 +41,7 @@ func BenchmarkSHA224(b *testing.B) {
4041
b.ResetTimer()
4142

4243
for i := 0; i < b.N; i++ {
43-
hash.SHA224(benchData)
44+
hash.SHA224(benchData, encoding.None)
4445
}
4546
}
4647

@@ -50,7 +51,7 @@ func BenchmarkSHA256(b *testing.B) {
5051
b.ResetTimer()
5152

5253
for i := 0; i < b.N; i++ {
53-
hash.SHA256(benchData)
54+
hash.SHA256(benchData, encoding.None)
5455
}
5556
}
5657

@@ -60,7 +61,7 @@ func BenchmarkSHA384(b *testing.B) {
6061
b.ResetTimer()
6162

6263
for i := 0; i < b.N; i++ {
63-
hash.SHA384(benchData)
64+
hash.SHA384(benchData, encoding.None)
6465
}
6566
}
6667

@@ -70,7 +71,7 @@ func BenchmarkSHA512(b *testing.B) {
7071
b.ResetTimer()
7172

7273
for i := 0; i < b.N; i++ {
73-
hash.SHA512(benchData)
74+
hash.SHA512(benchData, encoding.None)
7475
}
7576
}
7677

@@ -150,7 +151,7 @@ func BenchmarkFnv128(b *testing.B) {
150151
b.ResetTimer()
151152

152153
for i := 0; i < b.N; i++ {
153-
hash.Fnv128(benchData)
154+
hash.Fnv128(benchData, encoding.None)
154155
}
155156
}
156157

@@ -160,6 +161,6 @@ func BenchmarkFnv128a(b *testing.B) {
160161
b.ResetTimer()
161162

162163
for i := 0; i < b.N; i++ {
163-
hash.Fnv128a(benchData)
164+
hash.Fnv128a(benchData, encoding.None)
164165
}
165166
}

_examples/rand.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ package main
77
import (
88
"fmt"
99

10-
"github.com/FishGoddess/cryptox"
10+
"github.com/FishGoddess/cryptox/bytes/rand"
1111
)
1212

1313
func main() {
1414
// We provide some rand functions for you.
1515
// If you want to generate some keys or ivs, just feel free to use them.
16-
bs := cryptox.GenerateBytes(32)
17-
fmt.Println(bs)
16+
bs := rand.Bytes(32)
17+
fmt.Printf("%s\n", bs)
1818

19-
str := cryptox.GenerateString(64)
20-
fmt.Println(str)
19+
str := rand.String(64)
20+
fmt.Printf("%s\n", str)
2121

2222
// Already have a byte slice? Try AppendBytes:
23-
bs = make(cryptox.Bytes, 0, 16)
24-
bs = cryptox.AppendBytes(bs, 16)
25-
fmt.Println(bs)
23+
bs = []byte{'a', 'b', 'c', '-'}
24+
bs = rand.AppendBytes(bs, 16)
25+
fmt.Printf("%s\n", bs)
2626
}

_examples/rand_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ package main
77
import (
88
"testing"
99

10-
"github.com/FishGoddess/cryptox"
10+
"github.com/FishGoddess/cryptox/bytes/rand"
1111
)
1212

13-
// go test -v -cover -count=1 -test.cpu=1 -run=^$ -bench=^BenchmarkGenerateBytes$
14-
func BenchmarkGenerateBytes(b *testing.B) {
13+
// go test -v -cover -run=^$ -bench=^BenchmarkBytes$
14+
func BenchmarkBytes(b *testing.B) {
1515
b.ReportAllocs()
1616
b.ResetTimer()
1717

1818
for i := 0; i < b.N; i++ {
19-
cryptox.GenerateBytes(16)
19+
rand.Bytes(16)
2020
}
2121
}
2222

23-
// go test -v -cover -count=1 -test.cpu=1 -run=^$ -bench=^BenchmarkGenerateString$
24-
func BenchmarkGenerateString(b *testing.B) {
23+
// go test -v -cover -run=^$ -bench=^BenchmarkString$
24+
func BenchmarkString(b *testing.B) {
2525
b.ReportAllocs()
2626
b.ResetTimer()
2727

2828
for i := 0; i < b.N; i++ {
29-
cryptox.GenerateString(16)
29+
rand.String(16)
3030
}
3131
}

aes/aes_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (tr *testResult) compareTo(bs cryptox.Bytes) error {
3939
return nil
4040
}
4141

42-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestNewBlock$
42+
// go test -v -cover -run=^TestNewBlock$
4343
func TestNewBlock(t *testing.T) {
4444
block, blockSize, err := newBlock(testKey)
4545
if err != nil {
@@ -64,7 +64,7 @@ func TestNewBlock(t *testing.T) {
6464
}
6565
}
6666

67-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestECB$
67+
// go test -v -cover -run=^TestECB$
6868
func TestECB(t *testing.T) {
6969
cases := map[string]*testResult{
7070
"": {
@@ -105,7 +105,7 @@ func TestECB(t *testing.T) {
105105
}
106106
}
107107

108-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCBC$
108+
// go test -v -cover -run=^TestCBC$
109109
func TestCBC(t *testing.T) {
110110
cases := map[string]*testResult{
111111
"": {
@@ -146,7 +146,7 @@ func TestCBC(t *testing.T) {
146146
}
147147
}
148148

149-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCFB$
149+
// go test -v -cover -run=^TestCFB$
150150
func TestCFB(t *testing.T) {
151151
cases := map[string]*testResult{
152152
"": {
@@ -187,7 +187,7 @@ func TestCFB(t *testing.T) {
187187
}
188188
}
189189

190-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestOFB$
190+
// go test -v -cover -run=^TestOFB$
191191
func TestOFB(t *testing.T) {
192192
cases := map[string]*testResult{
193193
"": {
@@ -228,7 +228,7 @@ func TestOFB(t *testing.T) {
228228
}
229229
}
230230

231-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCTR$
231+
// go test -v -cover -run=^TestCTR$
232232
func TestCTR(t *testing.T) {
233233
cases := map[string]*testResult{
234234
"": {
@@ -269,7 +269,7 @@ func TestCTR(t *testing.T) {
269269
}
270270
}
271271

272-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestGCM$
272+
// go test -v -cover -run=^TestGCM$
273273
func TestGCM(t *testing.T) {
274274
cases := map[string]*testResult{
275275
"123": {

aes/gcm.go

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

aes/nonce.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2024 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 aes
6+
7+
import "github.com/FishGoddess/cryptox/bytes/rand"
8+
9+
// Nonce returns a standard nonce for gcm.
10+
func Nonce() []byte {
11+
nonceSize := 12
12+
return rand.Bytes(nonceSize)
13+
}

0 commit comments

Comments
 (0)