-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1d6d82e
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/longxiaoLX/goutils | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |