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