Java SecureRandom getProvider()方法及示例

Java SecureRandom getProvider()方法及示例

java.security.SecureRandom 类的 getProvider() 方法被用来返回这个SecureRandom对象的提供者。

语法

public final Provider getProvider()

返回值: 该方法返回该SecureRandom对象的提供者。

下面是说明getProvider()方法的例子。

例1 :

// Java program to demonstrate
// getProvider() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  
            // getting the Provider of the SecureRandom sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // printing the provider name
            System.out.println("Provider name : " + provider.getName());
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Provider name : SUN

例2 :

// Java program to demonstrate
// getProvider() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });
  
            // getting the Provider of the SecureRandom sr
            // by using method getProvider()
            Provider provider = srand.getProvider();
  
            // printing the provider name
            System.out.println("Provider name : " + provider.getName());
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Provider name : SUN

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程