1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 15:52:27 +08:00

feat: add Md5Byte

This commit is contained in:
dudaodong
2023-06-29 10:33:29 +08:00
parent 16c2df711b
commit 7d56da8108
5 changed files with 85 additions and 16 deletions

View File

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

View File

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

View File

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