Java中的SecureRandom getAlgorithm()方法

可以使用getAlgorithm()类java.security.SecureRandom中的方法获取SecureRandom对象的算法名称。此方法不需要任何参数,它返回SecureRandom对象的算法名称。

演示此的程序如下所示-

示例

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");
      }
   }
}

输出结果

The Algorithm is: SHA1PRNG

现在让我们了解上面的程序。

该方法getAlgorithm()用于获取SecureRandom对象的算法的名称。然后显示此算法名称。如果算法名称错误,则会引发异常NoSuchAlgorithmException。演示的代码片段如下-

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");
}