0

I wrote a backup program using Deflater and SHA-1 to store files and a hash value. I see that Java's Deflater uses zlib. If I explicit set the Deflater's level, could I expect to always get the same series of bytes regardless of platform and JRE version?

If not then what do I use? Are there any stable and fast pure-Java implementations?

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Stig
  • 1,974
  • 2
  • 23
  • 50

1 Answers1

2

Do the SHA-1 before compression. Then you verify the correctness of the compression and decompression as well.

There is no assurance that what a compressor produces today will be the same as what a later version of the compressor produces tomorrow for the same input. And there should be no such assurance, since that would preclude improvements in compression.

The only assurance is that the compression-decompression process is lossless, so that what you get from the decompressor is exactly what you fed the compressor. For that reason, you need to compute signatures on the input of the compressor and the output of the decompressor. Ignore the intermediate compressed stream.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158