SecureRandom getAlgorithm() method in Java


The name of the algorithm for the SecureRandom object can be obtained using the method getAlgorithm() in the class java.security.SecureRandom. This method requires no parameters and it returns the name of the algorithm for the SecureRandom 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 {
         SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
         String algorithmName = sRandom.getAlgorithm();
         System.out.println("The Algorithm is: " + algorithmName);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Algorithm is: SHA1PRNG

Now let us understand the above program.

The method getAlgorithm() is used to obtain the name of the algorithm for the SecureRandom 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 {
   SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
   String algorithmName = sRandom.getAlgorithm();
   System.out.println("The Algorithm is: " + algorithmName);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements