Skip to content

Commit da5e3f6

Browse files
committed
调整 hash 包使用姿势
2 parents be3971c + 9cc3a7d commit da5e3f6

File tree

12 files changed

+214
-317
lines changed

12 files changed

+214
-317
lines changed

FUTURE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
### v0.5.x
44

5-
* [ ] 支持 ECC key 生成
6-
* [ ] 支持 ECC 非对称加密算法
7-
* [ ] 支持更多的散列算法
5+
* [ ] 增加 cmd 包,提供二进制 cli 使用
86

97
### v0.4.x
108

119
* [ ] 支持 SM3 国标算法
1210
* [ ] 支持 SM4 国标算法
11+
* [ ] 支持 ECC key 生成
12+
* [ ] 支持 ECC 非对称加密算法
13+
* [ ] 支持更多的散列算法
1314
* [ ] 继续完善单元测试,提升覆盖率到 90%
1415

1516
### v0.3.x
1617

1718
* [x] 简化代码设计,提高代码可读性和可维护性
1819
* [x] 初步完善注释和单元测试,收尾重构工作
19-
* [ ] 增加 cmd 包,提供二进制 cli 使用
2020
* [x] ~~支持 GCM 加解密模式~~(该模式暂无法保证足够的测试验证,使用需谨慎)
2121

2222
### v0.2.x

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 📜 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.4.0
4+
5+
> 此版本发布于 2023-07-23
6+
7+
* 调整 hash 包使用姿势
8+
39
### v0.3.3
410

511
> 此版本发布于 2023-06-24

README.en.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ goos: darwin
4747
goarch: amd64
4848
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
4949
50-
BenchmarkMD5-12 6937862 166.6 ns/op 112 B/op 2 allocs/op
51-
BenchmarkSHA1-12 6311574 187.3 ns/op 136 B/op 2 allocs/op
52-
BenchmarkSHA224-12 4770352 251.5 ns/op 160 B/op 2 allocs/op
53-
BenchmarkSHA256-12 4743789 251.3 ns/op 160 B/op 2 allocs/op
54-
BenchmarkSHA384-12 3708129 334.7 ns/op 272 B/op 2 allocs/op
55-
BenchmarkSHA512-12 3557325 335.9 ns/op 288 B/op 2 allocs/op
56-
BenchmarkHMAC-12 1000000 1030 ns/op 512 B/op 6 allocs/op
57-
BenchmarkCRC32IEEE-12 16118478 71.73 ns/op 24 B/op 2 allocs/op
58-
BenchmarkCRC64ISO-12 25652527 45.25 ns/op 8 B/op 1 allocs/op
59-
BenchmarkCRC64ECMA-12 26552835 44.71 ns/op 8 B/op 1 allocs/op
60-
BenchmarkFnv32-12 40867548 28.54 ns/op 8 B/op 1 allocs/op
61-
BenchmarkFnv32a-12 41241873 28.21 ns/op 8 B/op 1 allocs/op
62-
BenchmarkFnv64-12 34188957 30.16 ns/op 8 B/op 1 allocs/op
63-
BenchmarkFnv64a-12 38607868 30.02 ns/op 8 B/op 1 allocs/op
64-
BenchmarkFnv128-12 23522612 50.60 ns/op 16 B/op 1 allocs/op
65-
BenchmarkFnv128a-12 23278514 51.00 ns/op 16 B/op 1 allocs/op
50+
BenchmarkMD5-12 11118327 111.2 ns/op 0 B/op 0 allocs/op
51+
BenchmarkSHA1-12 9216342 129.3 ns/op 0 B/op 0 allocs/op
52+
BenchmarkSHA224-12 6227264 219.6 ns/op 0 B/op 0 allocs/op
53+
BenchmarkSHA256-12 5364110 203.8 ns/op 0 B/op 0 allocs/op
54+
BenchmarkSHA384-12 4600442 254.7 ns/op 0 B/op 0 allocs/op
55+
BenchmarkSHA512-12 4683705 266.1 ns/op 0 B/op 0 allocs/op
56+
BenchmarkHMAC-12 929726 1156 ns/op 512 B/op 6 allocs/op
57+
BenchmarkCRC32IEEE-12 53518428 25.81 ns/op 0 B/op 0 allocs/op
58+
BenchmarkCRC64ISO-12 47010480 23.54 ns/op 0 B/op 0 allocs/op
59+
BenchmarkCRC64ECMA-12 51956055 23.19 ns/op 0 B/op 0 allocs/op
60+
BenchmarkFnv32-12 104025846 12.84 ns/op 0 B/op 0 allocs/op
61+
BenchmarkFnv32a-12 89485070 12.40 ns/op 0 B/op 0 allocs/op
62+
BenchmarkFnv64-12 68797173 15.69 ns/op 0 B/op 0 allocs/op
63+
BenchmarkFnv64a-12 78190952 16.05 ns/op 0 B/op 0 allocs/op
64+
BenchmarkFnv128-12 17743756 63.86 ns/op 16 B/op 1 allocs/op
65+
BenchmarkFnv128a-12 19056975 64.67 ns/op 16 B/op 1 allocs/op
6666
6767
BenchmarkDESEncryptECB-12 704928 1724 ns/op 528 B/op 3 allocs/op
6868
BenchmarkDESEncryptCBC-12 615673 1906 ns/op 624 B/op 6 allocs/op

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ goos: darwin
4747
goarch: amd64
4848
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
4949
50-
BenchmarkMD5-12 6937862 166.6 ns/op 112 B/op 2 allocs/op
51-
BenchmarkSHA1-12 6311574 187.3 ns/op 136 B/op 2 allocs/op
52-
BenchmarkSHA224-12 4770352 251.5 ns/op 160 B/op 2 allocs/op
53-
BenchmarkSHA256-12 4743789 251.3 ns/op 160 B/op 2 allocs/op
54-
BenchmarkSHA384-12 3708129 334.7 ns/op 272 B/op 2 allocs/op
55-
BenchmarkSHA512-12 3557325 335.9 ns/op 288 B/op 2 allocs/op
56-
BenchmarkHMAC-12 1000000 1030 ns/op 512 B/op 6 allocs/op
57-
BenchmarkCRC32IEEE-12 16118478 71.73 ns/op 24 B/op 2 allocs/op
58-
BenchmarkCRC64ISO-12 25652527 45.25 ns/op 8 B/op 1 allocs/op
59-
BenchmarkCRC64ECMA-12 26552835 44.71 ns/op 8 B/op 1 allocs/op
60-
BenchmarkFnv32-12 40867548 28.54 ns/op 8 B/op 1 allocs/op
61-
BenchmarkFnv32a-12 41241873 28.21 ns/op 8 B/op 1 allocs/op
62-
BenchmarkFnv64-12 34188957 30.16 ns/op 8 B/op 1 allocs/op
63-
BenchmarkFnv64a-12 38607868 30.02 ns/op 8 B/op 1 allocs/op
64-
BenchmarkFnv128-12 23522612 50.60 ns/op 16 B/op 1 allocs/op
65-
BenchmarkFnv128a-12 23278514 51.00 ns/op 16 B/op 1 allocs/op
50+
BenchmarkMD5-12 11118327 111.2 ns/op 0 B/op 0 allocs/op
51+
BenchmarkSHA1-12 9216342 129.3 ns/op 0 B/op 0 allocs/op
52+
BenchmarkSHA224-12 6227264 219.6 ns/op 0 B/op 0 allocs/op
53+
BenchmarkSHA256-12 5364110 203.8 ns/op 0 B/op 0 allocs/op
54+
BenchmarkSHA384-12 4600442 254.7 ns/op 0 B/op 0 allocs/op
55+
BenchmarkSHA512-12 4683705 266.1 ns/op 0 B/op 0 allocs/op
56+
BenchmarkHMAC-12 929726 1156 ns/op 512 B/op 6 allocs/op
57+
BenchmarkCRC32IEEE-12 53518428 25.81 ns/op 0 B/op 0 allocs/op
58+
BenchmarkCRC64ISO-12 47010480 23.54 ns/op 0 B/op 0 allocs/op
59+
BenchmarkCRC64ECMA-12 51956055 23.19 ns/op 0 B/op 0 allocs/op
60+
BenchmarkFnv32-12 104025846 12.84 ns/op 0 B/op 0 allocs/op
61+
BenchmarkFnv32a-12 89485070 12.40 ns/op 0 B/op 0 allocs/op
62+
BenchmarkFnv64-12 68797173 15.69 ns/op 0 B/op 0 allocs/op
63+
BenchmarkFnv64a-12 78190952 16.05 ns/op 0 B/op 0 allocs/op
64+
BenchmarkFnv128-12 17743756 63.86 ns/op 16 B/op 1 allocs/op
65+
BenchmarkFnv128a-12 19056975 64.67 ns/op 16 B/op 1 allocs/op
6666
6767
BenchmarkDESEncryptECB-12 704928 1724 ns/op 528 B/op 3 allocs/op
6868
BenchmarkDESEncryptCBC-12 615673 1906 ns/op 624 B/op 6 allocs/op

_examples/hash.go

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ func main() {
1717

1818
// All hashing functions will return a cryptox.Bytes type which can be encoded to hex and base64.
1919

20-
md5, _ := hash.MD5(data)
20+
md5 := hash.MD5(data)
2121
fmt.Println("md5 hex:", md5.Hex())
2222
fmt.Println("md5 base64:", md5.Base64())
2323

24-
sha1, _ := hash.SHA1(data)
24+
sha1 := hash.SHA1(data)
2525
fmt.Println("sha1 hex:", sha1.Hex())
2626
fmt.Println("sha1 base64:", sha1.Base64())
2727

28-
sha224, _ := hash.SHA224(data)
28+
sha224 := hash.SHA224(data)
2929
fmt.Println("sha224 hex:", sha224.Hex())
3030
fmt.Println("sha224 base64:", sha224.Base64())
3131

32-
sha256, _ := hash.SHA256(data)
32+
sha256 := hash.SHA256(data)
3333
fmt.Println("sha256 hex:", sha256.Hex())
3434
fmt.Println("sha256 base64:", sha256.Base64())
3535

36-
sha384, _ := hash.SHA384(data)
36+
sha384 := hash.SHA384(data)
3737
fmt.Println("sha384 hex:", sha384.Hex())
3838
fmt.Println("sha384 base64:", sha384.Base64())
3939

40-
sha512, _ := hash.SHA512(data)
40+
sha512 := hash.SHA512(data)
4141
fmt.Println("sha512 hex:", sha512.Hex())
4242
fmt.Println("sha512 base64:", sha512.Base64())
4343

@@ -47,46 +47,32 @@ func main() {
4747
fmt.Printf("hmac with key %s hex: %s\n", key, hmac.Hex())
4848
fmt.Printf("hmac with key %s base64: %s\n", key, hmac.Base64())
4949

50-
crc, crc32, _ := hash.CRC32IEEE(data)
50+
crc32 := hash.CRC32IEEE(data)
5151
fmt.Printf("crc32 with ieee: %d\n", crc32)
52-
fmt.Printf("crc32 with ieee hex: %s\n", crc.Hex())
53-
fmt.Printf("crc32 with ieee base64: %s\n", crc.Base64())
5452

55-
crc, crc64, _ := hash.CRC64ISO(data)
53+
crc64 := hash.CRC64ISO(data)
5654
fmt.Printf("crc64 with iso: %d\n", crc64)
57-
fmt.Printf("crc64 with iso hex: %s\n", crc.Hex())
58-
fmt.Printf("crc64 with iso base64: %s\n", crc.Base64())
5955

60-
crc, crc64, _ = hash.CRC64ECMA(data)
56+
crc64 = hash.CRC64ECMA(data)
6157
fmt.Printf("crc64 with ecma: %d\n", crc64)
62-
fmt.Printf("crc64 with ecma hex: %s\n", crc.Hex())
63-
fmt.Printf("crc64 with ecma base64: %s\n", crc.Base64())
6458

65-
fnv, fnv32, _ := hash.Fnv32(data)
59+
fnv32 := hash.Fnv32(data)
6660
fmt.Printf("fnv-1/32: %d\n", fnv32)
67-
fmt.Printf("fnv-1/32 hex: %s\n", fnv.Hex())
68-
fmt.Printf("fnv-1/32 base64: %s\n", fnv.Base64())
6961

70-
fnv, fnv32, _ = hash.Fnv32a(data)
71-
fmt.Printf("fnv-1/32a: %d\n", fnv32)
72-
fmt.Printf("fnv-1/32a hex: %s\n", fnv.Hex())
73-
fmt.Printf("fnv-1/32a base64: %s\n", fnv.Base64())
62+
fnv32a := hash.Fnv32a(data)
63+
fmt.Printf("fnv-1/32a: %d\n", fnv32a)
7464

75-
fnv, fnv64, _ := hash.Fnv64(data)
65+
fnv64 := hash.Fnv64(data)
7666
fmt.Printf("fnv-1/64: %d\n", fnv64)
77-
fmt.Printf("fnv-1/64 hex: %s\n", fnv.Hex())
78-
fmt.Printf("fnv-1/64 base64: %s\n", fnv.Base64())
7967

80-
fnv, fnv64, _ = hash.Fnv64a(data)
81-
fmt.Printf("fnv-1/64a: %d\n", fnv64)
82-
fmt.Printf("fnv-1/64a hex: %s\n", fnv.Hex())
83-
fmt.Printf("fnv-1/64a base64: %s\n", fnv.Base64())
68+
fnv64a := hash.Fnv64a(data)
69+
fmt.Printf("fnv-1/64a: %d\n", fnv64a)
8470

85-
fnv, _ = hash.Fnv128(data)
86-
fmt.Printf("fnv-1/128 hex: %s\n", fnv.Hex())
87-
fmt.Printf("fnv-1/128 base64: %s\n", fnv.Base64())
71+
fnv128 := hash.Fnv128(data)
72+
fmt.Printf("fnv-1/128 hex: %s\n", fnv128.Hex())
73+
fmt.Printf("fnv-1/128 base64: %s\n", fnv128.Base64())
8874

89-
fnv, _ = hash.Fnv128a(data)
90-
fmt.Printf("fnv-1/128a hex: %s\n", fnv.Hex())
91-
fmt.Printf("fnv-1/128a base64: %s\n", fnv.Base64())
75+
fnv128a := hash.Fnv128a(data)
76+
fmt.Printf("fnv-1/128a hex: %s\n", fnv128a.Hex())
77+
fmt.Printf("fnv-1/128a base64: %s\n", fnv128a.Base64())
9278
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

hash/crc.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,55 @@
55
package hash
66

77
import (
8-
stdcrc32 "hash/crc32"
9-
stdcrc64 "hash/crc64"
8+
"hash/crc32"
9+
"hash/crc64"
1010

1111
"github.com/FishGoddess/cryptox"
1212
)
1313

1414
var (
15-
tableIEEE = stdcrc32.IEEETable
16-
tableISO = stdcrc64.MakeTable(stdcrc64.ISO)
17-
tableECMA = stdcrc64.MakeTable(stdcrc64.ECMA)
15+
tableIEEE = crc32.IEEETable
16+
tableISO = crc64.MakeTable(crc64.ISO)
17+
tableECMA = crc64.MakeTable(crc64.ECMA)
1818
)
1919

2020
// Table32 is an alias of crc32.Table.
21-
type Table32 = stdcrc32.Table
21+
type Table32 = crc32.Table
2222

2323
// Table64 is an alias of crc64.Table.
24-
type Table64 = stdcrc64.Table
24+
type Table64 = crc64.Table
2525

2626
// CRC32 uses given table to checksum bs.
2727
// Use IEEE table if passed table is nil.
28-
func CRC32(bs cryptox.Bytes, table *Table32) (cryptox.Bytes, uint32, error) {
28+
func CRC32(bs cryptox.Bytes, table *Table32) uint32 {
2929
if table == nil {
3030
table = tableIEEE
3131
}
3232

33-
hash32 := stdcrc32.New(table)
34-
if _, err := hash32.Write(bs); err != nil {
35-
return nil, 0, err
36-
}
37-
38-
return hash32.Sum(nil), hash32.Sum32(), nil
33+
return crc32.Checksum(bs, table)
3934
}
4035

4136
// CRC32IEEE uses ieee table to checksum bs.
42-
func CRC32IEEE(bs cryptox.Bytes) (cryptox.Bytes, uint32, error) {
37+
func CRC32IEEE(bs cryptox.Bytes) uint32 {
4338
return CRC32(bs, tableIEEE)
4439
}
4540

4641
// CRC64 uses given table to checksum bs.
4742
// Use ISO table if passed table is nil.
48-
func CRC64(bs cryptox.Bytes, table *Table64) (cryptox.Bytes, uint64, error) {
43+
func CRC64(bs cryptox.Bytes, table *Table64) uint64 {
4944
if table == nil {
5045
table = tableISO
5146
}
5247

53-
hash64 := stdcrc64.New(table)
54-
if _, err := hash64.Write(bs); err != nil {
55-
return nil, 0, err
56-
}
57-
58-
return hash64.Sum(nil), hash64.Sum64(), nil
48+
return crc64.Checksum(bs, table)
5949
}
6050

6151
// CRC64ISO uses iso table to checksum bs.
62-
func CRC64ISO(bs cryptox.Bytes) (cryptox.Bytes, uint64, error) {
52+
func CRC64ISO(bs cryptox.Bytes) uint64 {
6353
return CRC64(bs, tableISO)
6454
}
6555

6656
// CRC64ECMA uses ecma table to checksum bs.
67-
func CRC64ECMA(bs cryptox.Bytes) (cryptox.Bytes, uint64, error) {
57+
func CRC64ECMA(bs cryptox.Bytes) uint64 {
6858
return CRC64(bs, tableECMA)
6959
}

0 commit comments

Comments
 (0)