Skip to content

Commit

Permalink
Update coord & hash
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Jan 18, 2024
1 parent 12f1a73 commit f5710bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions hash/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ package hash
import (
"crypto"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
)

// HMacSHA1 计算hmac-sha1值
func HMacSHA1(key, str string) string {
h := hmac.New(sha1.New, []byte(key))
h.Write([]byte(str))

return hex.EncodeToString(h.Sum(nil))
}

// HMacSHA256 计算hmac-sha256值
func HMacSHA256(key, str string) string {
h := hmac.New(sha256.New, []byte(key))
Expand Down
4 changes: 4 additions & 0 deletions hash/hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/stretchr/testify/assert"
)

func TestHMacSHA1(t *testing.T) {
assert.Equal(t, "30c0c496355c2bb9308c63159cc4b726f1205dfc", HMacSHA1("iiinsomnia", "ILoveYiigo"))
}

func TestHMacSHA256(t *testing.T) {
assert.Equal(t, "a458409cd884140c1ca36ef3013a5c7289c3e057049e3563401094d3f929b93b", HMacSHA256("iiinsomnia", "ILoveYiigo"))
}
Expand Down

0 comments on commit f5710bb

Please sign in to comment.