From 7d56da810894f8559daf4a47a46b0e6df745489c Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 29 Jun 2023 10:33:29 +0800 Subject: [PATCH] feat: add Md5Byte --- cryptor/basic.go | 8 ++++++++ cryptor/basic_test.go | 6 ++++++ cryptor/crypto_example_test.go | 27 +++++++++++---------------- docs/cryptor.md | 30 ++++++++++++++++++++++++++++++ docs/cryptor_zh-CN.md | 30 ++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 16 deletions(-) diff --git a/cryptor/basic.go b/cryptor/basic.go index 51e0ad7..e190e20 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -40,6 +40,14 @@ func Md5String(s string) string { return hex.EncodeToString(h.Sum(nil)) } +// Md5String return the md5 string of byte slice. +// Play: todo +func Md5Byte(data []byte) string { + h := md5.New() + h.Write(data) + return hex.EncodeToString(h.Sum(nil)) +} + // Md5File return the md5 value of file. func Md5File(filename string) (string, error) { if fileInfo, err := os.Stat(filename); err != nil { diff --git a/cryptor/basic_test.go b/cryptor/basic_test.go index d3e3228..b049820 100644 --- a/cryptor/basic_test.go +++ b/cryptor/basic_test.go @@ -21,6 +21,12 @@ func TestMd5String(t *testing.T) { assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello")) } +func TestMd5Byte(t *testing.T) { + assert := internal.NewAssert(t, "TestMd5Byte") + data := []byte{'a'} + assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data)) +} + func TestMd5File(t *testing.T) { fileMd5, err := Md5File("./basic.go") assert := internal.NewAssert(t, "TestMd5File") diff --git a/cryptor/crypto_example_test.go b/cryptor/crypto_example_test.go index d4352e0..4da1eda 100644 --- a/cryptor/crypto_example_test.go +++ b/cryptor/crypto_example_test.go @@ -366,21 +366,22 @@ func ExampleHmacSha512() { } func ExampleMd5String() { - str := "hello" - - md5Str := Md5String(str) - + md5Str := Md5String("hello") fmt.Println(md5Str) // Output: // 5d41402abc4b2a76b9719d911017c592 } +func ExampleMd5Byte() { + md5Str := Md5Byte([]byte{'a'}) + fmt.Println(md5Str) + + // Output: + // 0cc175b9c0f1b6a831c399e269772661 +} func ExampleSha1() { - str := "hello" - - result := Sha1(str) - + result := Sha1("hello") fmt.Println(result) // Output: @@ -388,10 +389,7 @@ func ExampleSha1() { } func ExampleSha256() { - str := "hello" - - result := Sha256(str) - + result := Sha256("hello") fmt.Println(result) // Output: @@ -399,10 +397,7 @@ func ExampleSha256() { } func ExampleSha512() { - str := "hello" - - result := Sha512(str) - + result := Sha512("hello") fmt.Println(result) // Output: diff --git a/docs/cryptor.md b/docs/cryptor.md index 735e15e..7cf707b 100644 --- a/docs/cryptor.md +++ b/docs/cryptor.md @@ -48,6 +48,7 @@ import ( - [HmacSha256](#HmacSha256) - [HmacSha512](#HmacSha512) - [Md5String](#Md5String) +- [Md5Byte](#Md5Byte) - [Md5File](#Md5File) - [Sha1](#Sha1) - [Sha256](#Sha256) @@ -880,6 +881,35 @@ func main() { } ``` +### Md5Byte + +

Return the md5 string of byte slice.

+ +Signature: + +```go +func Md5Byte(data []byte) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + md5Str := cryptor.Md5Byte([]byte{'a'}) + fmt.Println(md5Str) + + // Output: + // 0cc175b9c0f1b6a831c399e269772661 +} +``` + ### Md5File

Get the md5 value of file.

diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index 1166634..ba6dc69 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -45,6 +45,7 @@ import ( - [HmacSha256](#HmacSha256) - [HmacSha512](#HmacSha512) - [Md5String](#Md5String) +- [Md5Byte](#Md5Byte) - [Md5File](#Md5File) - [Sha1](#Sha1) - [Sha256](#Sha256) @@ -879,6 +880,35 @@ func main() { } ``` +### Md5Byte + +

获取byte slice的md5至。

+ +函数签名: + +```go +func Md5Byte(data []byte) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + md5Str := cryptor.Md5Byte([]byte{'a'}) + fmt.Println(md5Str) + + // Output: + // 0cc175b9c0f1b6a831c399e269772661 +} +``` + ### Md5File

获取文件md5值。