From 5b82b7c06d3f66b093472852daf3762325b534b2 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 16 Feb 2020 09:39:00 +0100 Subject: [PATCH] Add checksum verification for chunk data --- cryptpng.go | 8 ++++---- pngUtils.go | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cryptpng.go b/cryptpng.go index 54ac289..e5a700e 100644 --- a/cryptpng.go +++ b/cryptpng.go @@ -109,14 +109,14 @@ func DecryptDataPng(f *os.File, fout *os.File) { salt = append(salt, saltChunk.data...) } var data []byte - for _, cryptChunk := range png.GetChunksByName(chunkName) { + for i, cryptChunk := range png.GetChunksByName(chunkName) { + if !cryptChunk.Verify() { + log.Fatalf("Corrupted chunk data, chunk #%d", i) + } data = append(data, cryptChunk.data...) } if len(data) > 0 { data, err = decryptData(data, salt) - if err != nil { - log.Println("\nThe provided password is probably incorrect.") - } check(err) _, err = fout.Write(data) check(err) diff --git a/pngUtils.go b/pngUtils.go index de6b231..89faac4 100644 --- a/pngUtils.go +++ b/pngUtils.go @@ -30,6 +30,15 @@ func (c *ChunkData) GetRaw() []byte { return raw } +// verifies the integrity of the chunks data using crc +func (c *ChunkData) Verify() bool { + var data []byte + data = append(data, []byte(c.name)...) + data = append(data, c.data...) + crc := crc32.ChecksumIEEE(data) + return c.crc == crc +} + type PngData struct { header []byte chunks []ChunkData