Preferred hash algorithm for pgp signatures in Java

Hashing is used in digital signatures. By default DidiSoft OpenPGP Library for Java uses SHA1. To set another hashing algorithm to be used for signing, we have to change the default one with the method PGPLib.setHash(hash). 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 effect on subsequent calls to all sign and signAndEncrypt 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 256:

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.SHA256);
 }
}

<< Back to Examples