Skip to content

Commit a94d4d9

Browse files
committed
hmac 包重构
1 parent 2e9757a commit a94d4d9

File tree

5 files changed

+176
-69
lines changed

5 files changed

+176
-69
lines changed

_examples/hmac.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,39 @@ import (
1212
)
1313

1414
func main() {
15-
key := []byte("12345678")
15+
key := []byte("key")
1616
fmt.Printf("key: %s\n", key)
1717

1818
data := []byte("你好,世界")
1919
fmt.Printf("data: %s\n", data)
2020

21-
md5Hex := hmac.MD5(key, data, encoding.Hex)
22-
md5Base64 := hmac.MD5(key, data, encoding.Base64)
21+
md5Hex := hmac.MD5(data, key, encoding.Hex)
22+
md5Base64 := hmac.MD5(data, key, encoding.Base64)
2323
fmt.Printf("md5 hex: %s\n", md5Hex)
2424
fmt.Printf("md5 base64: %s\n", md5Base64)
2525

26-
sha1Hex := hmac.SHA1(key, data, encoding.Hex)
27-
sha1Base64 := hmac.SHA1(key, data, encoding.Base64)
26+
sha1Hex := hmac.SHA1(data, key, encoding.Hex)
27+
sha1Base64 := hmac.SHA1(data, key, encoding.Base64)
2828
fmt.Printf("sha1 hex: %s\n", sha1Hex)
2929
fmt.Printf("sha1 base64: %s\n", sha1Base64)
3030

31-
sha224Hex := hmac.SHA224(key, data, encoding.Hex)
32-
sha224Base64 := hmac.SHA224(key, data, encoding.Base64)
31+
sha224Hex := hmac.SHA224(data, key, encoding.Hex)
32+
sha224Base64 := hmac.SHA224(data, key, encoding.Base64)
3333
fmt.Printf("sha224 hex: %s\n", sha224Hex)
3434
fmt.Printf("sha224 base64: %s\n", sha224Base64)
3535

36-
sha256Hex := hmac.SHA256(key, data, encoding.Hex)
37-
sha256Base64 := hmac.SHA256(key, data, encoding.Base64)
36+
sha256Hex := hmac.SHA256(data, key, encoding.Hex)
37+
sha256Base64 := hmac.SHA256(data, key, encoding.Base64)
3838
fmt.Printf("sha256 hex: %s\n", sha256Hex)
3939
fmt.Printf("sha256 base64: %s\n", sha256Base64)
4040

41-
sha384Hex := hmac.SHA384(key, data, encoding.Hex)
42-
sha384Base64 := hmac.SHA384(key, data, encoding.Base64)
41+
sha384Hex := hmac.SHA384(data, key, encoding.Hex)
42+
sha384Base64 := hmac.SHA384(data, key, encoding.Base64)
4343
fmt.Printf("sha384 hex: %s\n", sha384Hex)
4444
fmt.Printf("sha384 base64: %s\n", sha384Base64)
4545

46-
sha512Hex := hmac.SHA512(key, data, encoding.Hex)
47-
sha512Base64 := hmac.SHA512(key, data, encoding.Base64)
46+
sha512Hex := hmac.SHA512(data, key, encoding.Hex)
47+
sha512Base64 := hmac.SHA512(data, key, encoding.Base64)
4848
fmt.Printf("sha512 hex: %s\n", sha512Hex)
4949
fmt.Printf("sha512 base64: %s\n", sha512Base64)
5050
}

_examples/hmac_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var (
15-
hmacBenchKey = []byte("12345678")
15+
hmacBenchKey = []byte("key")
1616
hmacBenchData = []byte("你好,世界")
1717
)
1818

@@ -22,7 +22,7 @@ func BenchmarkHMAC_MD5(b *testing.B) {
2222
b.ResetTimer()
2323

2424
for i := 0; i < b.N; i++ {
25-
hmac.MD5(hmacBenchKey, hmacBenchData, encoding.None)
25+
hmac.MD5(hmacBenchData, hmacBenchKey, encoding.None)
2626
}
2727
}
2828

@@ -32,7 +32,7 @@ func BenchmarkHMAC_SHA1(b *testing.B) {
3232
b.ResetTimer()
3333

3434
for i := 0; i < b.N; i++ {
35-
hmac.SHA1(hmacBenchKey, hmacBenchData, encoding.None)
35+
hmac.SHA1(hmacBenchData, hmacBenchKey, encoding.None)
3636
}
3737
}
3838

@@ -42,7 +42,7 @@ func BenchmarkHMAC_SHA224(b *testing.B) {
4242
b.ResetTimer()
4343

4444
for i := 0; i < b.N; i++ {
45-
hmac.SHA224(hmacBenchKey, hmacBenchData, encoding.None)
45+
hmac.SHA224(hmacBenchData, hmacBenchKey, encoding.None)
4646
}
4747
}
4848

@@ -52,7 +52,7 @@ func BenchmarkHMAC_SHA256(b *testing.B) {
5252
b.ResetTimer()
5353

5454
for i := 0; i < b.N; i++ {
55-
hmac.SHA256(hmacBenchKey, hmacBenchData, encoding.None)
55+
hmac.SHA256(hmacBenchData, hmacBenchKey, encoding.None)
5656
}
5757
}
5858

@@ -62,7 +62,7 @@ func BenchmarkHMAC_SHA384(b *testing.B) {
6262
b.ResetTimer()
6363

6464
for i := 0; i < b.N; i++ {
65-
hmac.SHA384(hmacBenchKey, hmacBenchData, encoding.None)
65+
hmac.SHA384(hmacBenchData, hmacBenchKey, encoding.None)
6666
}
6767
}
6868

@@ -72,6 +72,6 @@ func BenchmarkHMAC_SHA512(b *testing.B) {
7272
b.ResetTimer()
7373

7474
for i := 0; i < b.N; i++ {
75-
hmac.SHA512(hmacBenchKey, hmacBenchData, encoding.None)
75+
hmac.SHA512(hmacBenchData, hmacBenchKey, encoding.None)
7676
}
7777
}

hash/md_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ type testCase struct {
2424
func testHash(name string, hash testHashFunc, testCases []testCase) error {
2525
for _, testCase := range testCases {
2626
// None
27-
got := hash([]byte(testCase.Data), encoding.None)
27+
got := hash(testCase.Data, encoding.None)
2828
if !slices.Equal(got, testCase.HashData) {
2929
return fmt.Errorf("%s data %q: got %+v != expect %+v", name, testCase.Data, got, testCase.HashData)
3030
}
3131

3232
// Hex
33-
got = hash([]byte(testCase.Data), encoding.Hex)
33+
got = hash(testCase.Data, encoding.Hex)
3434
if !slices.Equal(got, testCase.HashDataHex) {
3535
return fmt.Errorf("%s data %q: got hex %s != expect hex %s", name, testCase.Data, got, testCase.HashDataHex)
3636
}
3737

3838
// Base64
39-
got = hash([]byte(testCase.Data), encoding.Base64)
39+
got = hash(testCase.Data, encoding.Base64)
4040
if !slices.Equal(got, testCase.HashDataBase64) {
4141
return fmt.Errorf("%s data %q: got base64 %s != expect base64 %s", name, testCase.Data, got, testCase.HashDataBase64)
4242
}

hmac/hmac.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
type hashFunc = func() stdhash.Hash
1919

20-
func hash(hashFunc hashFunc, key []byte, bs []byte, encoding encoding.Encoding) []byte {
20+
func hash(hashFunc hashFunc, bs []byte, key []byte, encoding encoding.Encoding) []byte {
2121
h := hmac.New(hashFunc, key)
2222
h.Write(bs)
2323

@@ -27,31 +27,31 @@ func hash(hashFunc hashFunc, key []byte, bs []byte, encoding encoding.Encoding)
2727
}
2828

2929
// MD5 uses hmac-md5 to hash bs and returns an error if failed.
30-
func MD5(key []byte, bs []byte, encoding encoding.Encoding) []byte {
31-
return hash(md5.New, key, bs, encoding)
30+
func MD5(bs []byte, key []byte, encoding encoding.Encoding) []byte {
31+
return hash(md5.New, bs, key, encoding)
3232
}
3333

3434
// SHA1 uses hmac-sha1 to hash bs and returns an error if failed.
35-
func SHA1(key []byte, bs []byte, encoding encoding.Encoding) []byte {
36-
return hash(sha1.New, key, bs, encoding)
35+
func SHA1(bs []byte, key []byte, encoding encoding.Encoding) []byte {
36+
return hash(sha1.New, bs, key, encoding)
3737
}
3838

3939
// SHA224 uses hmac-sha224 to hash bs and returns an error if failed.
40-
func SHA224(key []byte, bs []byte, encoding encoding.Encoding) []byte {
41-
return hash(sha256.New224, key, bs, encoding)
40+
func SHA224(bs []byte, key []byte, encoding encoding.Encoding) []byte {
41+
return hash(sha256.New224, bs, key, encoding)
4242
}
4343

4444
// SHA256 uses hmac-sha256 to hash bs and returns an error if failed.
45-
func SHA256(key []byte, bs []byte, encoding encoding.Encoding) []byte {
46-
return hash(sha256.New, key, bs, encoding)
45+
func SHA256(bs []byte, key []byte, encoding encoding.Encoding) []byte {
46+
return hash(sha256.New, bs, key, encoding)
4747
}
4848

4949
// SHA384 uses hmac-sha384 to hash bs and returns an error if failed.
50-
func SHA384(key []byte, bs []byte, encoding encoding.Encoding) []byte {
51-
return hash(sha512.New384, key, bs, encoding)
50+
func SHA384(bs []byte, key []byte, encoding encoding.Encoding) []byte {
51+
return hash(sha512.New384, bs, key, encoding)
5252
}
5353

5454
// SHA512 uses hmac-sha512 to hash bs and returns an error if failed.
55-
func SHA512(key []byte, bs []byte, encoding encoding.Encoding) []byte {
56-
return hash(sha512.New, key, bs, encoding)
55+
func SHA512(bs []byte, key []byte, encoding encoding.Encoding) []byte {
56+
return hash(sha512.New, bs, key, encoding)
5757
}

0 commit comments

Comments
 (0)