mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add HmacMd5WithBase64
This commit is contained in:
@@ -104,6 +104,14 @@ func HmacMd5(str, key string) string {
|
|||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
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.
|
// HmacSha1 return the hmac hash of string use sha1.
|
||||||
// Play: https://go.dev/play/p/1UI4oQ4WXKM
|
// Play: https://go.dev/play/p/1UI4oQ4WXKM
|
||||||
func HmacSha1(str, key string) string {
|
func HmacSha1(str, key string) string {
|
||||||
|
|||||||
@@ -65,6 +65,13 @@ func TestHmacMd5(t *testing.T) {
|
|||||||
assert.Equal("5f4c9faaff0a1ad3007d9ddc06abe36d", HmacMd5("hello world", "12345"))
|
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) {
|
func TestHmacSha1(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -328,6 +328,17 @@ func ExampleHmacMd5() {
|
|||||||
// e834306eab892d872525d4918a7a639a
|
// e834306eab892d872525d4918a7a639a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleHmacMd5WithBase64() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := HmacMd5WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 6DQwbquJLYclJdSRinpjmg==
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleHmacSha1() {
|
func ExampleHmacSha1() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ import (
|
|||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
|
- [HmacMd5WithBase64](#HmacMd5WithBase64)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
@@ -738,7 +739,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacMd5(data, key string) string
|
func HmacMd5(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -752,7 +753,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := cryptor.HmacMd5(str, key)
|
hms := cryptor.HmacMd5(str, key)
|
||||||
@@ -762,6 +763,39 @@ func main() {
|
|||||||
// e834306eab892d872525d4918a7a639a
|
// 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>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 hmac hash of string.</p>
|
<p>Get the sha1 hmac hash of string.</p>
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import (
|
|||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
|
- [HmacMd5WithBase64](#HmacMd5WithBase64)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
@@ -737,7 +738,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacMd5(data, key string) string
|
func HmacMd5(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -751,7 +752,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := cryptor.HmacMd5(str, key)
|
hms := cryptor.HmacMd5(str, key)
|
||||||
@@ -761,6 +762,38 @@ func main() {
|
|||||||
// e834306eab892d872525d4918a7a639a
|
// 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>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>获取字符串的sha1 hmac值。</p>
|
<p>获取字符串的sha1 hmac值。</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user