From e069bc0e9b191352c5960931b373b46b3f801e76 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Wed, 16 Mar 2022 14:47:47 +0100 Subject: [PATCH] Use SHA1 streaming hash for Etag --- storage/filesystem.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/storage/filesystem.go b/storage/filesystem.go index 6f3a189..24f8465 100644 --- a/storage/filesystem.go +++ b/storage/filesystem.go @@ -2,10 +2,11 @@ package storage import ( "context" - "crypto/md5" + "crypto/sha1" "encoding/base64" "encoding/json" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -75,11 +76,18 @@ func (b *filesystemBackend) safePath(ctx context.Context, path string) (string, } func etagForFile(path string) (string, error) { - data, err := ioutil.ReadFile(path) + f, err := os.Open(path) if err != nil { return "", err } - csum := md5.Sum(data) + defer f.Close() + + h := sha1.New() + if _, err := io.Copy(h, f); err != nil { + return "", err + } + csum := h.Sum(nil) + return base64.StdEncoding.EncodeToString(csum[:]), nil }