📌  相关文章
📜  Java中的 SecureRandom getProvider() 方法及示例

📅  最后修改于: 2022-05-13 01:54:22.237000             🧑  作者: Mango

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