29 lines
442 B
Go
29 lines
442 B
Go
package md5x
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// 字符串的md5
|
|
func Md5String(value string) string {
|
|
m := md5.New()
|
|
m.Write([]byte(value))
|
|
return hex.EncodeToString(m.Sum(nil))
|
|
}
|
|
|
|
// 文件的md5
|
|
func Md5File(path string) (string, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
hash := md5.New()
|
|
io.Copy(hash, file)
|
|
return hex.EncodeToString(hash.Sum(nil)), nil
|
|
}
|