Skip to content

Commit

Permalink
重构 hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Apr 27, 2024
1 parent 99ac942 commit 675ffed
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
10 changes: 5 additions & 5 deletions bytes/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const words = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var source = rand.NewSource(time.Now().UnixNano())

// GenerateToBytes generates n bytes in random and appends them to bs.
func GenerateToBytes(bs []byte, n int) []byte {
// AppendRandomBytes appends n random bytes to bs.
func AppendRandomBytes(bs []byte, n int) []byte {
length := int64(len(words))

for i := 0; i < n; i++ {
Expand All @@ -26,15 +26,15 @@ func GenerateToBytes(bs []byte, n int) []byte {
return bs
}

// GenerateBytes generates n bytes in random and returns them.
// GenerateBytes generates n random bytes and returns them.
func GenerateBytes(n int) []byte {
bs := make([]byte, 0, n)
bs = GenerateToBytes(bs, n)
bs = AppendRandomBytes(bs, n)

return bs
}

// GenerateString generates a string in random and returns it.
// GenerateString generates a random string and returns it.
func GenerateString(n int) string {
bs := GenerateBytes(n)

Expand Down
6 changes: 3 additions & 3 deletions bytes/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"testing"
)

// go test -v -cover -count=1 -test.cpu=1 -run=^TestGenerateToBytes$
func TestGenerateToBytes(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestAppendRandomBytes$
func TestAppendRandomBytes(t *testing.T) {
n := 32

for i := 0; i < 10; i++ {
bs := make([]byte, 0, n)
bs = GenerateToBytes(bs, n)
bs = AppendRandomBytes(bs, n)

if len(bs) != n {
t.Fatalf(" len(bs) %d != n %d", len(bs), n)
Expand Down
26 changes: 26 additions & 0 deletions hmac/hmac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package hmac

import (
stdhmac "crypto/hmac"
"fmt"
"hash"
)

func hmac(hashFunc func() hash.Hash, key []byte, bs []byte) ([]byte, error) {
hm := stdhmac.New(hashFunc, key)

n, err := hm.Write(bs)
if err != nil {
return nil, err
}

if n != len(bs) {
return nil, fmt.Errorf("cryptox: hmac written n %d != len(bs) %d", n, len(bs))
}

return hm.Sum(nil), nil
}
33 changes: 33 additions & 0 deletions hmac/hmac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package hmac

import (
"crypto/md5"
"encoding/hex"
"testing"
)

// go test -v -cover -run=^TestHMAC$
func TestHMAC(t *testing.T) {
testCases := map[string]string{
"": "63530468a04e386459855da0063b6596",
"123": "52851cb05258c8d98da1672d95729e53",
"你好,世界": "e76d8f84103533dc5d22a6e00cef74f3",
}

key := []byte("key")
for input, expect := range testCases {
sum, err := hmac(md5.New, key, []byte(input))
if err != nil {
t.Error(err)
}

sumHex := hex.EncodeToString(sum)
if sumHex != expect {
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect)
}
}
}
33 changes: 33 additions & 0 deletions hmac/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package hmac

import (
"crypto/md5"

"github.com/FishGoddess/cryptox/bytes"
)

func MD5(key []byte, bs []byte) ([]byte, error) {
return hmac(md5.New, key, bs)
}

func MD5Hex(key []byte, bs []byte) (string, error) {
hm, err := hmac(md5.New, key, bs)
if err != nil {
return "", err
}

return bytes.Hex(hm), nil
}

func MD5Base64(key []byte, bs []byte) (string, error) {
hm, err := hmac(md5.New, key, bs)
if err != nil {
return "", err
}

return bytes.Base64(hm), nil
}
32 changes: 32 additions & 0 deletions hmac/md5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package hmac

import (
"encoding/hex"
"testing"
)

// go test -v -cover -run=^TestMD5$
func TestMD5(t *testing.T) {
testCases := map[string]string{
"": "63530468a04e386459855da0063b6596",
"123": "52851cb05258c8d98da1672d95729e53",
"你好,世界": "e76d8f84103533dc5d22a6e00cef74f3",
}

key := []byte("key")
for input, expect := range testCases {
sum, err := MD5(key, []byte(input))
if err != nil {
t.Error(err)
}

sumHex := hex.EncodeToString(sum)
if sumHex != expect {
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect)
}
}
}

0 comments on commit 675ffed

Please sign in to comment.