当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DigestOutputStream getMessageDigest()用法及代码示例


java.security.DigestOutputStream类的getMessageDigest()方法提供分配给该DigestOutputStream对象的MessageDigest。

用法:

public MessageDigest getMessageDigest()

返回值:此方法返回分配给它的MessageDigest对象。


注意:由于不存在“名称”文件,因此本文中的所有程序都无法在在线IDE上运行。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在系统上创建一个文件“名称”。

下面是说明getMessageDigest()方法的示例:

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        try { 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By using getInstance() method 
            MessageDigest sr 
                = MessageDigest.getInstance("MD5"); 
  
            // creating and initializing 
            // object of OutputStream 
            OutputStream is 
                = new FileOutputStream("f:/java/name.txt"); 
  
            // creating and initializing 
            // object of DigestOutputStream 
            DigestOutputStream di 
                = new DigestOutputStream(is, sr); 
  
            // getting the message digest 
            // using getMessageDigest() method 
            MessageDigest str 
                = di.getMessageDigest(); 
  
            // display the result 
            System.out.println("Status : "
                               + str.toString()); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (FileNotFoundException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Status : MD5 Message Digest from SUN, 

示例2:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        try { 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By using getInstance() method 
            MessageDigest sr 
                = MessageDigest.getInstance("SHA-1"); 
  
            // creating and initializing 
            // object of OutputStream 
            OutputStream is 
                = new FileOutputStream("f:/java/name.txt"); 
  
            // creating and initializing 
            // object of DigestOutputStream 
            DigestOutputStream di 
                = new DigestOutputStream(is, sr); 
  
            // getting the message digest 
            // using getMessageDigest() method 
            MessageDigest str 
                = di.getMessageDigest(); 
  
            // display the result 
            System.out.println("Status : "
                               + str.toString()); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (FileNotFoundException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Status : SHA-1 Message Digest from SUN, 

参考: https://docs.oracle.com/javase/9/docs/api/java/security/DigestOutputStream.html#getMessageDigest–



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DigestOutputStream getMessageDigest() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。