Files
forever-files/fileUtilities/hash.go
2023-07-18 18:21:12 -06:00

24 lines
421 B
Go

package fileUtilities
import (
"crypto/sha256"
"fmt"
"io"
"os"
)
func HashFile(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return []byte{}, fmt.Errorf("error opening file for hashing: %w", err)
}
defer file.Close()
h := sha256.New()
if _, err := io.Copy(h, file); err != nil {
return []byte{}, fmt.Errorf("error hashing file: %w", err)
}
return h.Sum(nil), nil
}