Java SecureRandom getAlgorithm()方法及示例
java.security.SecureRandom 类的 getAlgorithm() 方法用于返回该SecureRandom对象实现的算法的名称。
语法:
public String getAlgorithm()
返回值: 该方法返回算法的名称,如果不能确定算法的名称,则返回未知值。
以下是说明 getAlgorithm()方法的例子:
例1:
// Java program to demonstrate// getAlgorithm() 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 Algorithm // by using method getAlgorithm() String algo = sr.getAlgorithm(); // printing the string algo System.out.println("Algorithm: " + algo); } catch (NoSuchAlgorithmException e) { System.out.println("Exception thrown : " + e); } }}
输出
Algorithm: SHA1PRNG
例2:
// Java program to demonstrate// getAlgorithm() 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("TAJMAHAL"); // getting the Algorithm // by using method getAlgorithm() String algo = sr.getAlgorithm(); // printing the string algo System.out.println(algo); } catch (NoSuchAlgorithmException e) { System.out.println("Exception thrown : " + e); } }}输出
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL SecureRandom not available
