XinYeYouKu/app/third/dfpOpenSdk/util/file_util.go

34 lines
469 B
Go
Raw Normal View History

2024-07-08 18:01:02 +08:00
package util
import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"log"
"os"
)
// FileHash 计算文件hash
func FileHash(filePath string) string {
_, err := os.Stat(filePath)
if err != nil {
log.Fatal(err)
return ""
}
if os.IsNotExist(err) {
log.Fatal(err)
return ""
}
file, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
return ""
}
h := sha256.New()
h.Write(file)
hash := h.Sum(nil)
return hex.EncodeToString(hash)
}