Compression preferred by the key
Each PGP key can hold compression algorithm preferences in a specific order. OpenPGP implementations that use this key must obey those preferences and pick one of them.
Compression preferred by the library
The library’s compression preferences can be customized. By default, DidiSoft OpenPGP Library for Java uses ZIP compression when encrypting and signing data.
When encrypting or signing data, the library preferences are matched to the preferences of the provided PGP key, and the first match is used. For example if the key preferred compression algorithms are “BZIP2, UNCOMPRESSED” and the library prefers “ZIP, UNCOMPRESSED” then the data won’t be compressed at all (UNCOMPRESSED). If there is no match then the first one of the library preferences will be used.
A list of available compression algorithms is available in the CompressionAlgorithm interface and CompressionAlgorithm.Enum
CompressionAlgorithm.Enum.ZIP CompressionAlgorithm.Enum.ZLIB CompressionAlgorithm.Enum.BZIP2 CompressionAlgorithm.Enum.UNCOMPRESSED |
Getting the current setting
We can get the current setting as String with the getCompression method
import com.didisoft.pgp.CompressionAlgorithm; import com.didisoft.pgp.PGPLib; public class GetCompressionDemo { public static void main(String[] args) throws Exception { PGPLib pgp = new PGPLib(); System.out.println(pgp.getCompression()); } } |
or as an array of enum values with the getCompressions method:
import com.didisoft.pgp.CompressionAlgorithm; import com.didisoft.pgp.PGPLib; public class GetCompressionDemo { public static void main(String[] args) throws Exception { PGPLib pgp = new PGPLib(); CompressionAlgorithm.Enum[] compressions = pgp.getCompressions(); } } |
Changing the current setting
In order to change the preferred compressions, we can either specify them as a list of comma-separated values with the setCompression method
import com.didisoft.pgp.CompressionAlgorithm; import com.didisoft.pgp.PGPLib; public class SetCompressionDemo { public static void main(String[] args) throws Exception { PGPLib pgp = new PGPLib(); pgp.setCompression(CompressionAlgorithm.ZLIB + "," + CompressionAlgorithm.ZIP); // each subsequent encryption will use ZLib or ZIP } } |
or as an array of enum values with the setCompressions method
import com.didisoft.pgp.CompressionAlgorithm; import com.didisoft.pgp.PGPLib; public class SetCompressionDemo { public static void main(String[] args) throws Exception { PGPLib pgp = new PGPLib(); pgp.setCompressions(new CompressionAlgorithm.Enum[] {CompressionAlgorithm.Enum.ZLIB, CompressionAlgorithm.Enum.ZIP}); // each subsequent encryption will use ZLib or ZIP } } |
Summary
This tutorial illustrated how DidiSoft OpenPGP Library for Java deals with the compression algorithm preferences and how you can customize them. Each PGP key has a set of preferred compressions and the one that will be picked will be decided based on the current settings of the library.