當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DigestInputStream.setMessageDigest()用法及代碼示例


java.security.DigestInputStream類的setMessageDigest()方法用於將指定的MessageDigest分配給DigestInputStream對象。

用法:

public void setMessageDigest(MessageDigest digest)

返回值:此方法無返回值。


注意:由於不存在“名稱”文件,因此本文中的所有程序都無法在在線IDE上運行。您可以在係統上的Java編譯器上檢查此代碼。要檢查此代碼,請在係統上創建一個文件“名稱”。

下麵是說明setMessageDigest()方法的示例:

示例1:

// Java program to demonstrate 
// setMessageDigest() 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 usng getInstance() method 
            MessageDigest sr 
                = MessageDigest.getInstance("MD5"); 
  
            // creating and intializing 
            // object of InputStream 
            InputStream is 
                = new FileInputStream( 
                    "f:/java/name.txt"); 
  
            // creating and intializing 
            // object of DigestInputStream 
            DigestInputStream di 
                = new DigestInputStream(is, sr); 
  
            // display the result 
            System.out.println( 
                "MessageDigest before assigning : "
                + di.getMessageDigest()); 
  
            // assigning the MessageDigest to this stream 
            // using setMessageDigest() method 
            di.setMessageDigest( 
                MessageDigest 
                    .getInstance("SHA-1")); 
  
            // display the result 
            System.out.println( 
                "MessageDigest after assigning : "
                + di.getMessageDigest()); 
        } 
  
        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); 
        } 
    } 
}
輸出:
MessageDigest before assigning : MD5 Message Digest from SUN, 

MessageDigest after assigning : SHA-1 Message Digest from SUN, 

示例2:

// Java program to demonstrate 
// setMessageDigest() 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 usng getInstance() method 
            MessageDigest sr 
                = MessageDigest.getInstance("SHA-1"); 
  
            // creating and intializing 
            // object of InputStream 
            InputStream is 
                = new FileInputStream( 
                    "f:/java/name.txt"); 
  
            // creating and intializing 
            // object of DigestInputStream 
            DigestInputStream di 
                = new DigestInputStream(is, sr); 
  
            // display the result 
            System.out.println( 
                "MessageDigest before assigning : "
                + di.getMessageDigest()); 
  
            // assigning the MessageDigest to this stream 
            // using setMessageDigest() method 
            di.setMessageDigest( 
                MessageDigest.getInstance("MD5")); 
  
            // display the result 
            System.out.println( 
                "MessageDigest after assigning : "
                + di.getMessageDigest()); 
        } 
  
        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); 
        } 
    } 
}
輸出:
MessageDigest before assigning : SHA-1 Message Digest from SUN, 

MessageDigest after assigning : MD5 Message Digest from SUN,  

參考: https://docs.oracle.com/javase/9/docs/api/java/security/DigestInputStream.html#setMessageDigest-java.security.MessageDigest-



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DigestInputStream.setMessageDigest() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。