mirror of
https://github.com/luxfi/util.git
synced 2026-10-30 15:13:56 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
42 lines
1022 B
Go
42 lines
1022 B
Go
// Copyright (C) 2022, Lux Industries Inc All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package utils
|
|
|
|
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.SplitSeq(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)
|
|
}
|