util/hashfile/hashfile.go (20 lines of code) (raw):
package hashfile
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"crypto/sha256"
"encoding/hex"
"io"
"os"
)
// SHA256 returns a sha256 hash of the file at the given filepath.
func SHA256(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
hString := hex.EncodeToString(h.Sum(nil))
return hString, nil
}