Skip to content

Commit

Permalink
推送
Browse files Browse the repository at this point in the history
  • Loading branch information
longxiaoLX committed May 16, 2023
0 parents commit 1d6d82e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/longxiaoLX/goutils

go 1.20
30 changes: 30 additions & 0 deletions hash/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hash

import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)

// get file md5
func FileMd5(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", fmt.Errorf("md5.go hash.FileMd5 os open error %v", err)
}
h := md5.New()
_, err = io.Copy(h, file)
if err != nil {
return "", fmt.Errorf("md5.go hash.FileMd5 io copy error %v", err)
}
return hex.EncodeToString(h.Sum(nil)), nil
}

// get string md5
func StringMd5(s string) string {
md5 := md5.New()
md5.Write([]byte(s))
return hex.EncodeToString(md5.Sum(nil))
}

0 comments on commit 1d6d82e

Please sign in to comment.