Preferred hash algorithm for pgp signatures in Java

Hashing is used in digital signatures. By default DidiSoft OpenPGP Library for Java uses SHA256.

To set another hashing algorithm to be used for signing, we have to change the default one with the method setHash of the PGPLib class.

The possible values are listed in the interface HashAlgorithm:

HashAlgorithm.SHA1
HashAlgorithm.SHA256
HashAlgorithm.SHA384
HashAlgorithm.SHA512
HashAlgorithm.SHA224
HashAlgorithm.MD5
HashAlgorithm.MD2
HashAlgorithm.RIPEMD160

The change will have an effect on subsequent calls to all sign and sign and encrypt methods for the current PGPLib instance. For each new instance, it has to be set explicitly again. This example shows how to set the hashing algorithm to SHA 512:

import com.didisoft.pgp.HashAlgorithm;
import com.didisoft.pgp.PGPLib;
 
public class SetHashDemo {
  public static void main(String[] args) throws Exception {
	PGPLib pgp = new PGPLib();
	pgp.setHash(HashAlgorithm.SHA512);
 }
}

<< Back to Examples