diff --git a/internal/util/checksum.go b/internal/util/checksum.go new file mode 100644 index 0000000..978380c --- /dev/null +++ b/internal/util/checksum.go @@ -0,0 +1,8 @@ +package util + +import "bytes" + +// CRC32Equal compares CRC32 checksums. +func CRC32Equal(b []byte, c uint32) bool { + return bytes.Equal(b, []byte{byte(0xff & (c >> 24)), byte(0xff & (c >> 16)), byte(0xff & (c >> 8)), byte(0xff & c)}) +} diff --git a/reader.go b/reader.go index 2a8753b..256cbd5 100644 --- a/reader.go +++ b/reader.go @@ -999,7 +999,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { } // CRC of the start header should match - if crc32Compare(h.Sum(nil), sh.CRC) != 0 { + if !util.CRC32Equal(h.Sum(nil), sh.CRC) { return errChecksum } @@ -1048,7 +1048,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { } // CRC should match the one from the start header - if crc32Compare(h.Sum(nil), start.CRC) != 0 { + if !util.CRC32Equal(h.Sum(nil), start.CRC) { return errChecksum } @@ -1080,7 +1080,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { } if cr, ok := fr.(checksumReadCloser); ok && crc != 0 { - if crc32Compare(cr.Checksum(), crc) != 0 { + if !util.CRC32Equal(cr.Checksum(), crc) { return errChecksum } }