Files
Willie 13744f89a8 refactor: consolidate duplicated utilities and simplify diff API
- Extract shared applyDetailScroll into tui_helpers.go (was duplicated
  across diff_tui.go, log_tui.go, audit_tui.go)
- Create utils.FileHash and utils.FileHashFormatted, replacing private
  copies in install/meta.go and audit/audit.go
- Export install.MetaFileName constant, use from diff_file.go
- Replace double SHA-256 in fileContentsEqual with streaming byte compare
  for early-exit efficiency
- Add 1MB size cap to generateUnifiedDiff to prevent performance issues
- Move noTUI into diffRenderOpts struct, simplifying function signatures
2026-03-02 02:23:50 +08:00

33 lines
617 B
Go

package utils
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
)
// FileHash returns the hex-encoded SHA-256 digest of a file.
func FileHash(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// FileHashFormatted returns the hash in "sha256:<hex>" format.
func FileHashFormatted(path string) (string, error) {
h, err := FileHash(path)
if err != nil {
return "", err
}
return fmt.Sprintf("sha256:%s", h), nil
}