Java DigestOutputStream.setMessageDigest()方法及示例

Java DigestOutputStream.setMessageDigest()方法及示例

java.security.DigestOutputStream 类的 setMessageDigest() 方法用于将指定的MessageDigest分配给DigestOutputStream对象。

语法

public void setMessageDigest(MessageDigest digest)

返回值: 该方法没有任何返回值。

注意: 本文中的所有程序都不会在在线IDE上运行,因为不存在’name’文件。你可以在你的系统的Java编译器上检查这段代码。要检查这段代码,请在你的系统上创建一个’name’文件。

以下是说明 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 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);
 
            // 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 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);
 
            // 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/DigestOutputStream.html#setMessageDigest-java.security.MessageDigest-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程