Skip to content

Commit

Permalink
调整 hash 包使用姿势
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Jul 22, 2023
2 parents be3971c + 9cc3a7d commit da5e3f6
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 317 deletions.
8 changes: 4 additions & 4 deletions FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

### v0.5.x

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

### v0.4.x

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

### v0.3.x

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

### v0.2.x
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 📜 历史版本的特性介绍 (Features in old versions)

### v0.4.0

> 此版本发布于 2023-07-23
* 调整 hash 包使用姿势

### v0.3.3

> 此版本发布于 2023-06-24
Expand Down
32 changes: 16 additions & 16 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkMD5-12 6937862 166.6 ns/op 112 B/op 2 allocs/op
BenchmarkSHA1-12 6311574 187.3 ns/op 136 B/op 2 allocs/op
BenchmarkSHA224-12 4770352 251.5 ns/op 160 B/op 2 allocs/op
BenchmarkSHA256-12 4743789 251.3 ns/op 160 B/op 2 allocs/op
BenchmarkSHA384-12 3708129 334.7 ns/op 272 B/op 2 allocs/op
BenchmarkSHA512-12 3557325 335.9 ns/op 288 B/op 2 allocs/op
BenchmarkHMAC-12 1000000 1030 ns/op 512 B/op 6 allocs/op
BenchmarkCRC32IEEE-12 16118478 71.73 ns/op 24 B/op 2 allocs/op
BenchmarkCRC64ISO-12 25652527 45.25 ns/op 8 B/op 1 allocs/op
BenchmarkCRC64ECMA-12 26552835 44.71 ns/op 8 B/op 1 allocs/op
BenchmarkFnv32-12 40867548 28.54 ns/op 8 B/op 1 allocs/op
BenchmarkFnv32a-12 41241873 28.21 ns/op 8 B/op 1 allocs/op
BenchmarkFnv64-12 34188957 30.16 ns/op 8 B/op 1 allocs/op
BenchmarkFnv64a-12 38607868 30.02 ns/op 8 B/op 1 allocs/op
BenchmarkFnv128-12 23522612 50.60 ns/op 16 B/op 1 allocs/op
BenchmarkFnv128a-12 23278514 51.00 ns/op 16 B/op 1 allocs/op
BenchmarkMD5-12 11118327 111.2 ns/op 0 B/op 0 allocs/op
BenchmarkSHA1-12 9216342 129.3 ns/op 0 B/op 0 allocs/op
BenchmarkSHA224-12 6227264 219.6 ns/op 0 B/op 0 allocs/op
BenchmarkSHA256-12 5364110 203.8 ns/op 0 B/op 0 allocs/op
BenchmarkSHA384-12 4600442 254.7 ns/op 0 B/op 0 allocs/op
BenchmarkSHA512-12 4683705 266.1 ns/op 0 B/op 0 allocs/op
BenchmarkHMAC-12 929726 1156 ns/op 512 B/op 6 allocs/op
BenchmarkCRC32IEEE-12 53518428 25.81 ns/op 0 B/op 0 allocs/op
BenchmarkCRC64ISO-12 47010480 23.54 ns/op 0 B/op 0 allocs/op
BenchmarkCRC64ECMA-12 51956055 23.19 ns/op 0 B/op 0 allocs/op
BenchmarkFnv32-12 104025846 12.84 ns/op 0 B/op 0 allocs/op
BenchmarkFnv32a-12 89485070 12.40 ns/op 0 B/op 0 allocs/op
BenchmarkFnv64-12 68797173 15.69 ns/op 0 B/op 0 allocs/op
BenchmarkFnv64a-12 78190952 16.05 ns/op 0 B/op 0 allocs/op
BenchmarkFnv128-12 17743756 63.86 ns/op 16 B/op 1 allocs/op
BenchmarkFnv128a-12 19056975 64.67 ns/op 16 B/op 1 allocs/op
BenchmarkDESEncryptECB-12 704928 1724 ns/op 528 B/op 3 allocs/op
BenchmarkDESEncryptCBC-12 615673 1906 ns/op 624 B/op 6 allocs/op
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkMD5-12 6937862 166.6 ns/op 112 B/op 2 allocs/op
BenchmarkSHA1-12 6311574 187.3 ns/op 136 B/op 2 allocs/op
BenchmarkSHA224-12 4770352 251.5 ns/op 160 B/op 2 allocs/op
BenchmarkSHA256-12 4743789 251.3 ns/op 160 B/op 2 allocs/op
BenchmarkSHA384-12 3708129 334.7 ns/op 272 B/op 2 allocs/op
BenchmarkSHA512-12 3557325 335.9 ns/op 288 B/op 2 allocs/op
BenchmarkHMAC-12 1000000 1030 ns/op 512 B/op 6 allocs/op
BenchmarkCRC32IEEE-12 16118478 71.73 ns/op 24 B/op 2 allocs/op
BenchmarkCRC64ISO-12 25652527 45.25 ns/op 8 B/op 1 allocs/op
BenchmarkCRC64ECMA-12 26552835 44.71 ns/op 8 B/op 1 allocs/op
BenchmarkFnv32-12 40867548 28.54 ns/op 8 B/op 1 allocs/op
BenchmarkFnv32a-12 41241873 28.21 ns/op 8 B/op 1 allocs/op
BenchmarkFnv64-12 34188957 30.16 ns/op 8 B/op 1 allocs/op
BenchmarkFnv64a-12 38607868 30.02 ns/op 8 B/op 1 allocs/op
BenchmarkFnv128-12 23522612 50.60 ns/op 16 B/op 1 allocs/op
BenchmarkFnv128a-12 23278514 51.00 ns/op 16 B/op 1 allocs/op
BenchmarkMD5-12 11118327 111.2 ns/op 0 B/op 0 allocs/op
BenchmarkSHA1-12 9216342 129.3 ns/op 0 B/op 0 allocs/op
BenchmarkSHA224-12 6227264 219.6 ns/op 0 B/op 0 allocs/op
BenchmarkSHA256-12 5364110 203.8 ns/op 0 B/op 0 allocs/op
BenchmarkSHA384-12 4600442 254.7 ns/op 0 B/op 0 allocs/op
BenchmarkSHA512-12 4683705 266.1 ns/op 0 B/op 0 allocs/op
BenchmarkHMAC-12 929726 1156 ns/op 512 B/op 6 allocs/op
BenchmarkCRC32IEEE-12 53518428 25.81 ns/op 0 B/op 0 allocs/op
BenchmarkCRC64ISO-12 47010480 23.54 ns/op 0 B/op 0 allocs/op
BenchmarkCRC64ECMA-12 51956055 23.19 ns/op 0 B/op 0 allocs/op
BenchmarkFnv32-12 104025846 12.84 ns/op 0 B/op 0 allocs/op
BenchmarkFnv32a-12 89485070 12.40 ns/op 0 B/op 0 allocs/op
BenchmarkFnv64-12 68797173 15.69 ns/op 0 B/op 0 allocs/op
BenchmarkFnv64a-12 78190952 16.05 ns/op 0 B/op 0 allocs/op
BenchmarkFnv128-12 17743756 63.86 ns/op 16 B/op 1 allocs/op
BenchmarkFnv128a-12 19056975 64.67 ns/op 16 B/op 1 allocs/op
BenchmarkDESEncryptECB-12 704928 1724 ns/op 528 B/op 3 allocs/op
BenchmarkDESEncryptCBC-12 615673 1906 ns/op 624 B/op 6 allocs/op
Expand Down
56 changes: 21 additions & 35 deletions _examples/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ func main() {

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

md5, _ := hash.MD5(data)
md5 := hash.MD5(data)
fmt.Println("md5 hex:", md5.Hex())
fmt.Println("md5 base64:", md5.Base64())

sha1, _ := hash.SHA1(data)
sha1 := hash.SHA1(data)
fmt.Println("sha1 hex:", sha1.Hex())
fmt.Println("sha1 base64:", sha1.Base64())

sha224, _ := hash.SHA224(data)
sha224 := hash.SHA224(data)
fmt.Println("sha224 hex:", sha224.Hex())
fmt.Println("sha224 base64:", sha224.Base64())

sha256, _ := hash.SHA256(data)
sha256 := hash.SHA256(data)
fmt.Println("sha256 hex:", sha256.Hex())
fmt.Println("sha256 base64:", sha256.Base64())

sha384, _ := hash.SHA384(data)
sha384 := hash.SHA384(data)
fmt.Println("sha384 hex:", sha384.Hex())
fmt.Println("sha384 base64:", sha384.Base64())

sha512, _ := hash.SHA512(data)
sha512 := hash.SHA512(data)
fmt.Println("sha512 hex:", sha512.Hex())
fmt.Println("sha512 base64:", sha512.Base64())

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

crc, crc32, _ := hash.CRC32IEEE(data)
crc32 := hash.CRC32IEEE(data)
fmt.Printf("crc32 with ieee: %d\n", crc32)
fmt.Printf("crc32 with ieee hex: %s\n", crc.Hex())
fmt.Printf("crc32 with ieee base64: %s\n", crc.Base64())

crc, crc64, _ := hash.CRC64ISO(data)
crc64 := hash.CRC64ISO(data)
fmt.Printf("crc64 with iso: %d\n", crc64)
fmt.Printf("crc64 with iso hex: %s\n", crc.Hex())
fmt.Printf("crc64 with iso base64: %s\n", crc.Base64())

crc, crc64, _ = hash.CRC64ECMA(data)
crc64 = hash.CRC64ECMA(data)
fmt.Printf("crc64 with ecma: %d\n", crc64)
fmt.Printf("crc64 with ecma hex: %s\n", crc.Hex())
fmt.Printf("crc64 with ecma base64: %s\n", crc.Base64())

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

fnv, fnv32, _ = hash.Fnv32a(data)
fmt.Printf("fnv-1/32a: %d\n", fnv32)
fmt.Printf("fnv-1/32a hex: %s\n", fnv.Hex())
fmt.Printf("fnv-1/32a base64: %s\n", fnv.Base64())
fnv32a := hash.Fnv32a(data)
fmt.Printf("fnv-1/32a: %d\n", fnv32a)

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

fnv, fnv64, _ = hash.Fnv64a(data)
fmt.Printf("fnv-1/64a: %d\n", fnv64)
fmt.Printf("fnv-1/64a hex: %s\n", fnv.Hex())
fmt.Printf("fnv-1/64a base64: %s\n", fnv.Base64())
fnv64a := hash.Fnv64a(data)
fmt.Printf("fnv-1/64a: %d\n", fnv64a)

fnv, _ = hash.Fnv128(data)
fmt.Printf("fnv-1/128 hex: %s\n", fnv.Hex())
fmt.Printf("fnv-1/128 base64: %s\n", fnv.Base64())
fnv128 := hash.Fnv128(data)
fmt.Printf("fnv-1/128 hex: %s\n", fnv128.Hex())
fmt.Printf("fnv-1/128 base64: %s\n", fnv128.Base64())

fnv, _ = hash.Fnv128a(data)
fmt.Printf("fnv-1/128a hex: %s\n", fnv.Hex())
fmt.Printf("fnv-1/128a base64: %s\n", fnv.Base64())
fnv128a := hash.Fnv128a(data)
fmt.Printf("fnv-1/128a hex: %s\n", fnv128a.Hex())
fmt.Printf("fnv-1/128a base64: %s\n", fnv128a.Base64())
}
4 changes: 2 additions & 2 deletions _icons/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 14 additions & 24 deletions hash/crc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,55 @@
package hash

import (
stdcrc32 "hash/crc32"
stdcrc64 "hash/crc64"
"hash/crc32"
"hash/crc64"

"github.com/FishGoddess/cryptox"
)

var (
tableIEEE = stdcrc32.IEEETable
tableISO = stdcrc64.MakeTable(stdcrc64.ISO)
tableECMA = stdcrc64.MakeTable(stdcrc64.ECMA)
tableIEEE = crc32.IEEETable
tableISO = crc64.MakeTable(crc64.ISO)
tableECMA = crc64.MakeTable(crc64.ECMA)
)

// Table32 is an alias of crc32.Table.
type Table32 = stdcrc32.Table
type Table32 = crc32.Table

// Table64 is an alias of crc64.Table.
type Table64 = stdcrc64.Table
type Table64 = crc64.Table

// CRC32 uses given table to checksum bs.
// Use IEEE table if passed table is nil.
func CRC32(bs cryptox.Bytes, table *Table32) (cryptox.Bytes, uint32, error) {
func CRC32(bs cryptox.Bytes, table *Table32) uint32 {
if table == nil {
table = tableIEEE
}

hash32 := stdcrc32.New(table)
if _, err := hash32.Write(bs); err != nil {
return nil, 0, err
}

return hash32.Sum(nil), hash32.Sum32(), nil
return crc32.Checksum(bs, table)
}

// CRC32IEEE uses ieee table to checksum bs.
func CRC32IEEE(bs cryptox.Bytes) (cryptox.Bytes, uint32, error) {
func CRC32IEEE(bs cryptox.Bytes) uint32 {
return CRC32(bs, tableIEEE)
}

// CRC64 uses given table to checksum bs.
// Use ISO table if passed table is nil.
func CRC64(bs cryptox.Bytes, table *Table64) (cryptox.Bytes, uint64, error) {
func CRC64(bs cryptox.Bytes, table *Table64) uint64 {
if table == nil {
table = tableISO
}

hash64 := stdcrc64.New(table)
if _, err := hash64.Write(bs); err != nil {
return nil, 0, err
}

return hash64.Sum(nil), hash64.Sum64(), nil
return crc64.Checksum(bs, table)
}

// CRC64ISO uses iso table to checksum bs.
func CRC64ISO(bs cryptox.Bytes) (cryptox.Bytes, uint64, error) {
func CRC64ISO(bs cryptox.Bytes) uint64 {
return CRC64(bs, tableISO)
}

// CRC64ECMA uses ecma table to checksum bs.
func CRC64ECMA(bs cryptox.Bytes) (cryptox.Bytes, uint64, error) {
func CRC64ECMA(bs cryptox.Bytes) uint64 {
return CRC64(bs, tableECMA)
}
Loading

0 comments on commit da5e3f6

Please sign in to comment.