Files
qvyun/backend/pkg/utils/md5.go
2025-01-16 15:59:21 +08:00

29 lines
452 B
Go

package utils
import (
"crypto/md5"
"encoding/hex"
"io"
"os"
)
// FileMd5 calculates the md5 of a file
func FileMd5(file string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func StringMd5(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}