diff --git a/internal/hashio/files.go b/internal/hashio/files.go index 4d1a32199..4dcdacc2f 100644 --- a/internal/hashio/files.go +++ b/internal/hashio/files.go @@ -25,24 +25,19 @@ func SHA256(filename string) (string, error) { // HashContent computes a SHA-256 hash of the file content combined with extra content, // and returns the first 16 characters of the hex-encoded hash. func HashContent(filename string, extra ...string) (string, error) { + h := sha256.New() + file, err := os.Open(filePath) if err != nil { return "", fmt.Errorf("failed to open file: %w", err) } defer file.Close() - fileInfo, err := file.Stat() - if err != nil { - return "", fmt.Errorf("failed to get file info: %w", err) - } - - buffer := make([]byte, fileInfo.Size()) - if _, err := file.Read(buffer); err != nil { + if _, err := io.Copy(h, file); err != nil { return "", fmt.Errorf("failed to read file: %w", err) } - combinedContent := string(buffer) + strings.Join(extraContent, "") + h.Write([]byte(strings.Join(extraContent, ""))) - hash := sha256.Sum256([]byte(combinedContent)) - return fmt.Sprintf("%x", hash)[:15], nil + return fmt.Sprintf("%x", h.Sum(nil))[:16], nil }