SecureRandom getProvider() method in Java

Java 8ProgrammingObject Oriented Programming

The provider for the SecureRandom object can be obtained using the method getProvider() in the class java.security.SecureRandom. This method requires no parameters and it returns the provider 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");
         Provider provider = sRandom.getProvider();
         System.out.println("The Provider is: " + provider.getName());
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Provider is: SUN

Now let us understand the above program.

The method getProvider() is used to obtain the provider for the SecureRandom object. Then this provider 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");
   Provider provider = sRandom.getProvider();
   System.out.println("The Provider is: " + provider.getName());
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}
raja
Updated on 30-Jul-2019 22:30:25

Advertisements