grype/internal/file/hasher.go

48 lines
1 KiB
Go
Raw Normal View History

2020-06-19 14:12:29 +00:00
package file
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"strings"
"github.com/spf13/afero"
)
func ValidateByHash(fs afero.Fs, path, hashStr string) (bool, string, error) {
2020-06-19 14:12:29 +00:00
var hasher hash.Hash
var hashFn string
2020-06-19 14:12:29 +00:00
switch {
case strings.HasPrefix(hashStr, "sha256:"):
hashFn = "sha256"
2020-06-19 14:12:29 +00:00
hasher = sha256.New()
default:
return false, "", fmt.Errorf("hasher not supported or specified (given: %s)", hashStr)
2020-06-19 14:12:29 +00:00
}
hashNoPrefix := strings.Split(hashStr, ":")[1]
actualHash, err := HashFile(fs, path, hasher)
if err != nil {
return false, "", err
2020-06-19 14:12:29 +00:00
}
return actualHash == hashNoPrefix, hashFn + ":" + actualHash, nil
2020-06-19 14:12:29 +00:00
}
func HashFile(fs afero.Fs, path string, hasher hash.Hash) (string, error) {
f, err := fs.Open(path)
if err != nil {
return "", fmt.Errorf("failed to open file '%s': %w", path, err)
}
defer f.Close()
if _, err := io.Copy(hasher, f); err != nil {
return "", fmt.Errorf("failed to hash file '%s': %w", path, err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}