Java Signature getAlgorithm() method

Java 8ProgrammingObject Oriented Programming

The name of the algorithm for the signature object can be obtained using the method getAlgorithm() in the class java.security.Signature. This method requires no parameters and it returns the name of the algorithm for the signature object.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) {
      try {
         Signature signature = Signature.getInstance("SHA256withRSA");
         String algorithm = signature.getAlgorithm();
         System.out.println("The Algorithm is: " + algorithm);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Algorithm is: SHA256withRSA

Now let us understand the above program.

The method getAlgorithm() is used to obtain the name of the algorithm for the signature object. Then this algorithm name is displayed. If the algorithm name is wrong, then the exception NoSuchAlgorithmException is thrown. A code snippet that demonstrates is given as follows −

try {
   Signature signature = Signature.getInstance("SHA256withRSA");
   String algorithm = signature.getAlgorithm();
   System.out.println("The Algorithm is: " + algorithm);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}
raja
Published on 12-Mar-2019 10:27:18
Advertisements