Files
crypto/hash/sha256_file.go

42 lines
1023 B
Go

// Copyright (C) 2022, Lux Partners Limited All rights reserved.
// See the file LICENSE for licensing terms.
package hash
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strings"
)
func GetSHA256FromDisk(binPath string) (string, error) {
if _, err := os.Stat(binPath); err != nil {
return "", fmt.Errorf("failed looking up plugin binary at %s: %w", binPath, err)
}
hasher := sha256.New()
s, err := os.ReadFile(binPath)
if err != nil {
return "", err
}
if _, err := hasher.Write(s); err != nil {
return "", fmt.Errorf("failed calculating the sha256 hash of the binary %s: %w", binPath, err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func SearchSHA256File(file []byte, toSearch string) (string, error) {
lines := strings.Split(string(file), "\n")
for _, line := range lines {
sha256Info := strings.Fields(line)
if len(sha256Info) == 2 {
if sha256Info[1] == toSearch {
return sha256Info[0], nil
}
}
}
return "", fmt.Errorf("%q not found in sha256 file", toSearch)
}