1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-20 03:59:26 +02:00
goreleaser/checksum/checksum.go

34 lines
643 B
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// Package checksum contain algorithms to checksum files
2017-04-14 13:50:25 -03:00
package checksum
import (
"crypto/sha256"
"encoding/hex"
"hash"
"io"
"os"
)
// SHA256 sum of the given file
func SHA256(path string) (string, error) {
2017-04-14 13:50:25 -03:00
return calculate(sha256.New(), path)
}
func calculate(hash hash.Hash, path string) (string, error) {
2017-04-14 13:50:25 -03:00
file, err := os.Open(path)
if err != nil {
return "", err
2017-04-14 13:50:25 -03:00
}
2017-12-17 23:04:29 -02:00
defer file.Close() // nolint: errcheck
2017-04-14 13:50:25 -03:00
2017-04-22 10:58:29 -03:00
return doCalculate(hash, file)
}
func doCalculate(hash hash.Hash, file io.Reader) (string, error) {
_, err := io.Copy(hash, file)
2017-04-14 13:50:25 -03:00
if err != nil {
return "", err
2017-04-14 13:50:25 -03:00
}
return hex.EncodeToString(hash.Sum(nil)), nil
2017-04-14 13:50:25 -03:00
}