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

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

Java中的 MessageDigest getAlgorithm() 方法及示例

Java.security.MessageDigest类的getAlgorithm()方法用于返回与此消息摘要关联的算法的标准名称。

句法:

public final 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 object of MessageDigest
            MessageDigest msd
                = MessageDigest.getInstance("MD5");
  
            // get the name of algorithm
            // used in MessageDigest
            // using getAlgorithm() method
            String algo = msd.getAlgorithm();
  
            // printing the string algo
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Algorithm : MD5

示例 2:

// Java program to demonstrate
// getAlgorithm() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating object of MessageDigest
            MessageDigest msd
                = MessageDigest.getInstance("SHA-256");
  
            // get the name of algorithm used in MessageDigest
            // using getAlgorithm() method
            String algo = msd.getAlgorithm();
  
            // printing the string algo
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Algorithm : SHA-256

参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/MessageDigest.html#getAlgorithm–