Skip to content

Commit 23743ae

Browse files
committed
重构 hash 和 hmac 包
1 parent f1e2121 commit 23743ae

File tree

11 files changed

+211
-366
lines changed

11 files changed

+211
-366
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ test:
99
go test -cover -count=1 -test.cpu=1 ./...
1010

1111
bench:
12-
go test -v ./_examples/rand_test.go -bench=. -benchtime=1s
1312
go test -v ./_examples/hash_test.go -bench=. -benchtime=1s
1413
go test -v ./_examples/hmac_test.go -bench=. -benchtime=1s
1514
go test -v ./_examples/des_test.go -bench=. -benchtime=1s

_examples/hash_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var (
1515
benchData = []byte("你好,世界")
1616
)
1717

18-
// go test -v -bench=^BenchmarkMD5$ -benchtime=1s hash_test.go
19-
func BenchmarkMD5(b *testing.B) {
18+
// go test -v -bench=^BenchmarkHash_MD5$ -benchtime=1s hash_test.go
19+
func BenchmarkHash_MD5(b *testing.B) {
2020
b.ReportAllocs()
2121
b.ResetTimer()
2222

@@ -25,8 +25,8 @@ func BenchmarkMD5(b *testing.B) {
2525
}
2626
}
2727

28-
// go test -v -bench=^BenchmarkSHA1$ -benchtime=1s hash_test.go
29-
func BenchmarkSHA1(b *testing.B) {
28+
// go test -v -bench=^BenchmarkHash_SHA1$ -benchtime=1s hash_test.go
29+
func BenchmarkHash_SHA1(b *testing.B) {
3030
b.ReportAllocs()
3131
b.ResetTimer()
3232

@@ -35,8 +35,8 @@ func BenchmarkSHA1(b *testing.B) {
3535
}
3636
}
3737

38-
// go test -v -bench=^BenchmarkSHA224$ -benchtime=1s hash_test.go
39-
func BenchmarkSHA224(b *testing.B) {
38+
// go test -v -bench=^BenchmarkHash_SHA224$ -benchtime=1s hash_test.go
39+
func BenchmarkHash_SHA224(b *testing.B) {
4040
b.ReportAllocs()
4141
b.ResetTimer()
4242

@@ -45,8 +45,8 @@ func BenchmarkSHA224(b *testing.B) {
4545
}
4646
}
4747

48-
// go test -v -bench=^BenchmarkSHA256$ -benchtime=1s hash_test.go
49-
func BenchmarkSHA256(b *testing.B) {
48+
// go test -v -bench=^BenchmarkHash_SHA256$ -benchtime=1s hash_test.go
49+
func BenchmarkHash_SHA256(b *testing.B) {
5050
b.ReportAllocs()
5151
b.ResetTimer()
5252

@@ -55,8 +55,8 @@ func BenchmarkSHA256(b *testing.B) {
5555
}
5656
}
5757

58-
// go test -v -bench=^BenchmarkSHA384$ -benchtime=1s hash_test.go
59-
func BenchmarkSHA384(b *testing.B) {
58+
// go test -v -bench=^BenchmarkHash_SHA384$ -benchtime=1s hash_test.go
59+
func BenchmarkHash_SHA384(b *testing.B) {
6060
b.ReportAllocs()
6161
b.ResetTimer()
6262

@@ -65,8 +65,8 @@ func BenchmarkSHA384(b *testing.B) {
6565
}
6666
}
6767

68-
// go test -v -bench=^BenchmarkSHA512$ -benchtime=1s hash_test.go
69-
func BenchmarkSHA512(b *testing.B) {
68+
// go test -v -bench=^BenchmarkHash_SHA512$ -benchtime=1s hash_test.go
69+
func BenchmarkHash_SHA512(b *testing.B) {
7070
b.ReportAllocs()
7171
b.ResetTimer()
7272

@@ -75,8 +75,8 @@ func BenchmarkSHA512(b *testing.B) {
7575
}
7676
}
7777

78-
// go test -v -bench=^BenchmarkCRC32IEEE$ -benchtime=1s hash_test.go
79-
func BenchmarkCRC32IEEE(b *testing.B) {
78+
// go test -v -bench=^BenchmarkHash_CRC32IEEE$ -benchtime=1s hash_test.go
79+
func BenchmarkHash_CRC32IEEE(b *testing.B) {
8080
b.ReportAllocs()
8181
b.ResetTimer()
8282

@@ -85,8 +85,8 @@ func BenchmarkCRC32IEEE(b *testing.B) {
8585
}
8686
}
8787

88-
// go test -v -bench=^BenchmarkCRC64ISO$ -benchtime=1s hash_test.go
89-
func BenchmarkCRC64ISO(b *testing.B) {
88+
// go test -v -bench=^BenchmarkHash_CRC64ISO$ -benchtime=1s hash_test.go
89+
func BenchmarkHash_CRC64ISO(b *testing.B) {
9090
b.ReportAllocs()
9191
b.ResetTimer()
9292

@@ -95,8 +95,8 @@ func BenchmarkCRC64ISO(b *testing.B) {
9595
}
9696
}
9797

98-
// go test -v -bench=^BenchmarkCRC64ECMA$ -benchtime=1s hash_test.go
99-
func BenchmarkCRC64ECMA(b *testing.B) {
98+
// go test -v -bench=^BenchmarkHash_CRC64ECMA$ -benchtime=1s hash_test.go
99+
func BenchmarkHash_CRC64ECMA(b *testing.B) {
100100
b.ReportAllocs()
101101
b.ResetTimer()
102102

@@ -105,8 +105,8 @@ func BenchmarkCRC64ECMA(b *testing.B) {
105105
}
106106
}
107107

108-
// go test -v -bench=^BenchmarkFnv32$ -benchtime=1s hash_test.go
109-
func BenchmarkFnv32(b *testing.B) {
108+
// go test -v -bench=^BenchmarkHash_Fnv32$ -benchtime=1s hash_test.go
109+
func BenchmarkHash_Fnv32(b *testing.B) {
110110
b.ReportAllocs()
111111
b.ResetTimer()
112112

@@ -115,8 +115,8 @@ func BenchmarkFnv32(b *testing.B) {
115115
}
116116
}
117117

118-
// go test -v -bench=^BenchmarkFnv32a$ -benchtime=1s hash_test.go
119-
func BenchmarkFnv32a(b *testing.B) {
118+
// go test -v -bench=^BenchmarkHash_Fnv32a$ -benchtime=1s hash_test.go
119+
func BenchmarkHash_Fnv32a(b *testing.B) {
120120
b.ReportAllocs()
121121
b.ResetTimer()
122122

@@ -125,8 +125,8 @@ func BenchmarkFnv32a(b *testing.B) {
125125
}
126126
}
127127

128-
// go test -v -bench=^BenchmarkFnv64$ -benchtime=1s hash_test.go
129-
func BenchmarkFnv64(b *testing.B) {
128+
// go test -v -bench=^BenchmarkHash_Fnv64$ -benchtime=1s hash_test.go
129+
func BenchmarkHash_Fnv64(b *testing.B) {
130130
b.ReportAllocs()
131131
b.ResetTimer()
132132

@@ -135,8 +135,8 @@ func BenchmarkFnv64(b *testing.B) {
135135
}
136136
}
137137

138-
// go test -v -bench=^BenchmarkFnv64a$ -benchtime=1s hash_test.go
139-
func BenchmarkFnv64a(b *testing.B) {
138+
// go test -v -bench=^BenchmarkHash_Fnv64a$ -benchtime=1s hash_test.go
139+
func BenchmarkHash_Fnv64a(b *testing.B) {
140140
b.ReportAllocs()
141141
b.ResetTimer()
142142

@@ -145,8 +145,8 @@ func BenchmarkFnv64a(b *testing.B) {
145145
}
146146
}
147147

148-
// go test -v -bench=^BenchmarkFnv128$ -benchtime=1s hash_test.go
149-
func BenchmarkFnv128(b *testing.B) {
148+
// go test -v -bench=^BenchmarkHash_Fnv128$ -benchtime=1s hash_test.go
149+
func BenchmarkHash_Fnv128(b *testing.B) {
150150
b.ReportAllocs()
151151
b.ResetTimer()
152152

@@ -155,8 +155,8 @@ func BenchmarkFnv128(b *testing.B) {
155155
}
156156
}
157157

158-
// go test -v -bench=^BenchmarkFnv128a$ -benchtime=1s hash_test.go
159-
func BenchmarkFnv128a(b *testing.B) {
158+
// go test -v -bench=^BenchmarkHash_Fnv128a$ -benchtime=1s hash_test.go
159+
func BenchmarkHash_Fnv128a(b *testing.B) {
160160
b.ReportAllocs()
161161
b.ResetTimer()
162162

_examples/hmac.go

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

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

1314
func main() {
1415
key := []byte("12345678")
15-
fmt.Println("key:", key)
16+
fmt.Printf("key: %s\n", key)
1617

1718
data := []byte("你好,世界")
18-
fmt.Println("data:", data)
19-
20-
md5, _ := hmac.MD5(key, data)
21-
fmt.Println("hmac md5 hex:", md5.Hex())
22-
fmt.Println("hmac md5 base64:", md5.Base64())
23-
24-
sha1, _ := hmac.SHA1(key, data)
25-
fmt.Println("hmac sha1 hex:", sha1.Hex())
26-
fmt.Println("hmac sha1 base64:", sha1.Base64())
27-
28-
sha224, _ := hmac.SHA224(key, data)
29-
fmt.Println("hmac sha224 hex:", sha224.Hex())
30-
fmt.Println("hmac sha224 base64:", sha224.Base64())
31-
32-
sha256, _ := hmac.SHA256(key, data)
33-
fmt.Println("hmac sha256 hex:", sha256.Hex())
34-
fmt.Println("hmac sha256 base64:", sha256.Base64())
35-
36-
sha384, _ := hmac.SHA384(key, data)
37-
fmt.Println("hmac sha384 hex:", sha384.Hex())
38-
fmt.Println("hmac sha384 base64:", sha384.Base64())
39-
40-
sha512, _ := hmac.SHA512(key, data)
41-
fmt.Println("hmac sha512 hex:", sha512.Hex())
42-
fmt.Println("hmac sha512 base64:", sha512.Base64())
19+
fmt.Printf("data: %s\n", data)
20+
21+
md5Hex := hmac.MD5(key, data, encoding.Hex)
22+
md5Base64 := hmac.MD5(key, data, encoding.Base64)
23+
fmt.Printf("md5 hex: %s\n", md5Hex)
24+
fmt.Printf("md5 base64: %s\n", md5Base64)
25+
26+
sha1Hex := hmac.SHA1(key, data, encoding.Hex)
27+
sha1Base64 := hmac.SHA1(key, data, encoding.Base64)
28+
fmt.Printf("sha1 hex: %s\n", sha1Hex)
29+
fmt.Printf("sha1 base64: %s\n", sha1Base64)
30+
31+
sha224Hex := hmac.SHA224(key, data, encoding.Hex)
32+
sha224Base64 := hmac.SHA224(key, data, encoding.Base64)
33+
fmt.Printf("sha224 hex: %s\n", sha224Hex)
34+
fmt.Printf("sha224 base64: %s\n", sha224Base64)
35+
36+
sha256Hex := hmac.SHA256(key, data, encoding.Hex)
37+
sha256Base64 := hmac.SHA256(key, data, encoding.Base64)
38+
fmt.Printf("sha256 hex: %s\n", sha256Hex)
39+
fmt.Printf("sha256 base64: %s\n", sha256Base64)
40+
41+
sha384Hex := hmac.SHA384(key, data, encoding.Hex)
42+
sha384Base64 := hmac.SHA384(key, data, encoding.Base64)
43+
fmt.Printf("sha384 hex: %s\n", sha384Hex)
44+
fmt.Printf("sha384 base64: %s\n", sha384Base64)
45+
46+
sha512Hex := hmac.SHA512(key, data, encoding.Hex)
47+
sha512Base64 := hmac.SHA512(key, data, encoding.Base64)
48+
fmt.Printf("sha512 hex: %s\n", sha512Hex)
49+
fmt.Printf("sha512 base64: %s\n", sha512Base64)
4350
}

_examples/hmac_test.go

Lines changed: 19 additions & 18 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/hmac"
1112
)
1213

@@ -15,62 +16,62 @@ var (
1516
hmacBenchData = []byte("你好,世界")
1617
)
1718

18-
// go test -v -bench=^BenchmarkHMACUsingMD5$ -benchtime=1s hash_test.go
19-
func BenchmarkHMACUsingMD5(b *testing.B) {
19+
// go test -v -bench=^BenchmarkHMAC_MD5$ -benchtime=1s hash_test.go
20+
func BenchmarkHMAC_MD5(b *testing.B) {
2021
b.ReportAllocs()
2122
b.ResetTimer()
2223

2324
for i := 0; i < b.N; i++ {
24-
hmac.MD5(hmacBenchKey, hmacBenchData)
25+
hmac.MD5(hmacBenchKey, hmacBenchData, encoding.None)
2526
}
2627
}
2728

28-
// go test -v -bench=^BenchmarkHMACUsingSHA1$ -benchtime=1s hash_test.go
29-
func BenchmarkHMACUsingSHA1(b *testing.B) {
29+
// go test -v -bench=^BenchmarkHMAC_SHA1$ -benchtime=1s hash_test.go
30+
func BenchmarkHMAC_SHA1(b *testing.B) {
3031
b.ReportAllocs()
3132
b.ResetTimer()
3233

3334
for i := 0; i < b.N; i++ {
34-
hmac.SHA1(hmacBenchKey, hmacBenchData)
35+
hmac.SHA1(hmacBenchKey, hmacBenchData, encoding.None)
3536
}
3637
}
3738

38-
// go test -v -bench=^BenchmarkHMACUsingSHA224$ -benchtime=1s hash_test.go
39-
func BenchmarkHMACUsingSHA224(b *testing.B) {
39+
// go test -v -bench=^BenchmarkHMAC_SHA224$ -benchtime=1s hash_test.go
40+
func BenchmarkHMAC_SHA224(b *testing.B) {
4041
b.ReportAllocs()
4142
b.ResetTimer()
4243

4344
for i := 0; i < b.N; i++ {
44-
hmac.SHA224(hmacBenchKey, hmacBenchData)
45+
hmac.SHA224(hmacBenchKey, hmacBenchData, encoding.None)
4546
}
4647
}
4748

48-
// go test -v -bench=^BenchmarkHMACUsingSHA256$ -benchtime=1s hash_test.go
49-
func BenchmarkHMACUsingSHA256(b *testing.B) {
49+
// go test -v -bench=^BenchmarkHMAC_SHA256$ -benchtime=1s hash_test.go
50+
func BenchmarkHMAC_SHA256(b *testing.B) {
5051
b.ReportAllocs()
5152
b.ResetTimer()
5253

5354
for i := 0; i < b.N; i++ {
54-
hmac.SHA256(hmacBenchKey, hmacBenchData)
55+
hmac.SHA256(hmacBenchKey, hmacBenchData, encoding.None)
5556
}
5657
}
5758

58-
// go test -v -bench=^BenchmarkHMACUsingSHA384$ -benchtime=1s hash_test.go
59-
func BenchmarkHMACUsingSHA384(b *testing.B) {
59+
// go test -v -bench=^BenchmarkHMAC_SHA384$ -benchtime=1s hash_test.go
60+
func BenchmarkHMAC_SHA384(b *testing.B) {
6061
b.ReportAllocs()
6162
b.ResetTimer()
6263

6364
for i := 0; i < b.N; i++ {
64-
hmac.SHA384(hmacBenchKey, hmacBenchData)
65+
hmac.SHA384(hmacBenchKey, hmacBenchData, encoding.None)
6566
}
6667
}
6768

68-
// go test -v -bench=^BenchmarkHMACUsingSHA512$ -benchtime=1s hash_test.go
69-
func BenchmarkHMACUsingSHA512(b *testing.B) {
69+
// go test -v -bench=^BenchmarkHMAC_SHA512$ -benchtime=1s hash_test.go
70+
func BenchmarkHMAC_SHA512(b *testing.B) {
7071
b.ReportAllocs()
7172
b.ResetTimer()
7273

7374
for i := 0; i < b.N; i++ {
74-
hmac.SHA512(hmacBenchKey, hmacBenchData)
75+
hmac.SHA512(hmacBenchKey, hmacBenchData, encoding.None)
7576
}
7677
}

_examples/rand.go

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

_examples/rand_test.go

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

0 commit comments

Comments
 (0)