Skip to content

Commit

Permalink
add hmacmd5 and hmacsha1
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpingcoder committed Jul 11, 2021
1 parent 699f5d6 commit 3202e68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions utils/cryptoutil/CryptoUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/jumpingcoder/quickutil4go/utils/logutil"
"strings"
Expand Down Expand Up @@ -34,6 +36,18 @@ func SHA1Encrypt(content []byte) string {
return fmt.Sprintf("%x", sum)
}

func HmacMD5(key string, data []byte) string {
hmac := hmac.New(md5.New, []byte(key))
hmac.Write(data)
return hex.EncodeToString(hmac.Sum([]byte("")))
}

func HmacSHA1(key string, data []byte) string {
hmac := hmac.New(sha1.New, []byte(key))
hmac.Write(data)
return hex.EncodeToString(hmac.Sum([]byte("")))
}

func AESCBCEncrypt(content []byte, key []byte, iv []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions utils/cryptoutil/CryptoUtil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ func TestDecryptConfigHandler(t *testing.T) {
func TestMD5Encrypt(t *testing.T) {
t.Log(MD5Encrypt([]byte("123456")))
}

func TestHmacMD5Encrypt(t *testing.T) {
t.Log(HmacMD5("1234",[]byte("123456")))
}

0 comments on commit 3202e68

Please sign in to comment.