From 456018c9c065f8bd824689b3c75bcf336bb81f89 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Tue, 7 Sep 2021 10:00:51 -0400 Subject: [PATCH] Increase hash size for Screens.Hash() Use a SHA256 hash of a serialized version of the render tree. This gives us a 32-byte hash instead of an 8-byte hash, reducing the likelihood of any collisions to effectively nil. --- encode/encode.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/encode/encode.go b/encode/encode.go index 257a089732..6f5ea3a474 100644 --- a/encode/encode.go +++ b/encode/encode.go @@ -2,7 +2,8 @@ package encode import ( "bytes" - "encoding/binary" + "crypto/sha256" + "encoding/json" "fmt" "image" "image/color" @@ -10,7 +11,6 @@ import ( "time" "github.com/harukasan/go-libwebp/webp" - "github.com/mitchellh/hashstructure/v2" "github.com/pkg/errors" "tidbyt.dev/pixlet/render" ) @@ -69,14 +69,13 @@ func (s *Screens) Hash() ([]byte, error) { hashable.Images = s.images } - sum, err := hashstructure.Hash(hashable, hashstructure.FormatV2, nil) + j, err := json.Marshal(hashable) if err != nil { - return nil, err + return nil, errors.Wrap(err, "marshaling render tree to JSON") } - buf := make([]byte, binary.MaxVarintLen64) - n := binary.PutUvarint(buf, sum) - return buf[:n], nil + h := sha256.Sum256(j) + return h[:], nil } // Renders a screen to WebP. Optionally pass filters for