pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/hash/filehash.go (about) 1 // Package hash contains different hash algorithms and utilities 2 package hash 3 4 // ////////////////////////////////////////////////////////////////////////////////// // 5 // // 6 // Copyright (c) 2022 ESSENTIAL KAOS // 7 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 8 // // 9 // ////////////////////////////////////////////////////////////////////////////////// // 10 11 import ( 12 "crypto/sha256" 13 "fmt" 14 "io" 15 "os" 16 ) 17 18 // ////////////////////////////////////////////////////////////////////////////////// // 19 20 // FileHash generates an SHA-256 hash for a given file 21 func FileHash(file string) string { 22 fd, err := os.OpenFile(file, os.O_RDONLY, 0) 23 24 if err != nil { 25 return "" 26 } 27 28 defer fd.Close() 29 30 hasher := sha256.New() 31 32 io.Copy(hasher, fd) 33 34 return fmt.Sprintf("%064x", hasher.Sum(nil)) 35 }