feat: add HmacMd5WithBase64

This commit is contained in:
dudaodong
2023-07-28 17:53:46 +08:00
parent 0ae5d17a06
commit 31eb5f4d1f
5 changed files with 97 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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"

View File

@@ -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() {
<b>Signature:</b>
```go
func HmacMd5(data, key string) string
func HmacMd5(str, key string) string
```
<b>Example:</b>
@@ -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
}
```
### <span id="HmacMd5WithBase64">HmacMd5WithBase64</span>
<p>Get the md5 hmac hash of base64 string.</p>
<b>Signature:</b>
```go
func HmacMd5WithBase64(str, key string) string
```
<b>Example:</b>
```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==
}
```
### <span id="HmacSha1">HmacSha1</span>
<p>Get the sha1 hmac hash of string.</p>

View File

@@ -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() {
<b>函数签名:</b>
```go
func HmacMd5(data, key string) string
func HmacMd5(str, key string) string
```
<b>示例:</b>
@@ -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
}
```
### <span id="HmacMd5WithBase64">HmacMd5WithBase64</span>
<p>获取字符串md5 hmac base64字符串值。</p>
<b>函数签名:</b>
```go
func HmacMd5WithBase64(str, key string) string
```
<b>示例:</b>
```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==
}
```
### <span id="HmacSha1">HmacSha1</span>
<p>获取字符串的sha1 hmac值。</p>