From a2444ce8a925506bbdba96b3bd73b0a306dada26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20W=C3=A5reus?= Date: Mon, 9 Oct 2023 11:34:25 +0200 Subject: [PATCH] normalize files before md5 --- internal/file/fingerprint.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/file/fingerprint.go b/internal/file/fingerprint.go index 81b14f22..d76639c1 100644 --- a/internal/file/fingerprint.go +++ b/internal/file/fingerprint.go @@ -4,7 +4,6 @@ import ( "bufio" "crypto/md5" // #nosec "fmt" - "io" "os" "path/filepath" "strings" @@ -149,19 +148,27 @@ func shouldProcessFile(fileInfo os.FileInfo, exclusions []string, path string) b return true } +func normalizeLineEndings(data []byte) []byte { + // Convert to Unix line endings + return []byte(strings.ReplaceAll(string(data), "\r\n", "\n")) +} + func computeMD5(filename string) (FileFingerprint, error) { - file, err := os.Open(filename) + data, err := os.ReadFile(filename) if err != nil { return FileFingerprint{}, err } - defer file.Close() + + normalizedData := normalizeLineEndings(data) hash := md5.New() // #nosec - if _, err := io.Copy(hash, file); err != nil { + + if _, err := hash.Write(normalizedData); err != nil { return FileFingerprint{}, err } - contentLength, err := file.Seek(0, 2) + contentLength := int64(len(normalizedData)) + if err != nil { return FileFingerprint{}, err }