diff --git a/cryptor/basic.go b/cryptor/basic.go index b37ff97..0c1dfea 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -104,6 +104,14 @@ func HmacMd5(str, key string) string { return hex.EncodeToString(h.Sum([]byte(""))) } +// HmacMd5WithBase64 return the hmac hash of string use md5 with base64. +// todo +func HmacMd5WithBase64(data, key string) string { + h := hmac.New(md5.New, []byte(key)) + h.Write([]byte(data)) + return base64.StdEncoding.EncodeToString(h.Sum([]byte(""))) +} + // HmacSha1 return the hmac hash of string use sha1. // Play: https://go.dev/play/p/1UI4oQ4WXKM func HmacSha1(str, key string) string { diff --git a/cryptor/basic_test.go b/cryptor/basic_test.go index add01e0..075d8af 100644 --- a/cryptor/basic_test.go +++ b/cryptor/basic_test.go @@ -65,6 +65,13 @@ func TestHmacMd5(t *testing.T) { assert.Equal("5f4c9faaff0a1ad3007d9ddc06abe36d", HmacMd5("hello world", "12345")) } +func TestHmacMd5WithBase64(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestHmacMd5WithBase64") + assert.Equal("6DQwbquJLYclJdSRinpjmg==", HmacMd5WithBase64("hello", "12345")) +} + func TestHmacSha1(t *testing.T) { t.Parallel() diff --git a/cryptor/crypto_example_test.go b/cryptor/crypto_example_test.go index 745a325..7266c95 100644 --- a/cryptor/crypto_example_test.go +++ b/cryptor/crypto_example_test.go @@ -328,6 +328,17 @@ func ExampleHmacMd5() { // e834306eab892d872525d4918a7a639a } +func ExampleHmacMd5WithBase64() { + str := "hello" + key := "12345" + + hms := HmacMd5WithBase64(str, key) + fmt.Println(hms) + + // Output: + // 6DQwbquJLYclJdSRinpjmg== +} + func ExampleHmacSha1() { str := "hello" key := "12345" diff --git a/docs/cryptor.md b/docs/cryptor.md index d3da8e8..d318c0b 100644 --- a/docs/cryptor.md +++ b/docs/cryptor.md @@ -44,6 +44,7 @@ import ( - [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbDecrypt](#DesOfbDecrypt) - [HmacMd5](#HmacMd5) +- [HmacMd5WithBase64](#HmacMd5WithBase64) - [HmacSha1](#HmacSha1) - [HmacSha1WithBase64](#HmacSha1WithBase64) - [HmacSha256](#HmacSha256) @@ -738,7 +739,7 @@ func main() { Signature: ```go -func HmacMd5(data, key string) string +func HmacMd5(str, key string) string ``` Example: @@ -752,7 +753,7 @@ import ( ) func main() { - str := "hello" + str := "hello" key := "12345" hms := cryptor.HmacMd5(str, key) @@ -762,6 +763,39 @@ func main() { // e834306eab892d872525d4918a7a639a } ``` + +### HmacMd5WithBase64 + +

Get the md5 hmac hash of base64 string.

+ +Signature: + +```go +func HmacMd5WithBase64(str, key string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + str := "hello" + key := "12345" + + hms := cryptor.HmacMd5WithBase64(str, key) + fmt.Println(hms) + + // Output: + // 6DQwbquJLYclJdSRinpjmg== +} +``` + ### HmacSha1

Get the sha1 hmac hash of string.

diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index adb5a53..d004563 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -41,6 +41,7 @@ import ( - [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbDecrypt](#DesOfbDecrypt) - [HmacMd5](#HmacMd5) +- [HmacMd5WithBase64](#HmacMd5WithBase64) - [HmacSha1](#HmacSha1) - [HmacSha1WithBase64](#HmacSha1WithBase64) - [HmacSha256](#HmacSha256) @@ -737,7 +738,7 @@ func main() { 函数签名: ```go -func HmacMd5(data, key string) string +func HmacMd5(str, key string) string ``` 示例: @@ -751,7 +752,7 @@ import ( ) func main() { - str := "hello" + str := "hello" key := "12345" hms := cryptor.HmacMd5(str, key) @@ -761,6 +762,38 @@ func main() { // e834306eab892d872525d4918a7a639a } ``` + +### HmacMd5WithBase64 + +

获取字符串md5 hmac base64字符串值。

+ +函数签名: + +```go +func HmacMd5WithBase64(str, key string) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + str := "hello" + key := "12345" + + hms := cryptor.HmacMd5WithBase64(str, key) + fmt.Println(hms) + + // Output: + // 6DQwbquJLYclJdSRinpjmg== +} +``` ### HmacSha1

获取字符串的sha1 hmac值。