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

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

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

Java.security.MessageDigest类的reset()方法用于将当前消息摘要值重置为此 MessageDisgest 对象的默认消息摘要值。
句法:

public void reset()

返回值:此方法没有任何返回值。
以下是说明reset()方法的示例:
示例 1:

Java
// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.nio.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            byte[] barr = { 10, 20, 30, 40 };
 
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("MD5");
 
            // display the digest value before Updation
            System.out.println("MessageDigest before update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
 
            // update MessageDigest value
            // using update() method
            msd1.update(bb);
 
            // display the digest value before Updation
            System.out.println("\nMessageDigest after update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // reset MessageDigest value
            // using reset() method
            msd1.reset();
 
            // display the digest value after reset
            System.out.println("\nMessageDigest after reset : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.nio.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            byte[] barr = { 10, 20, 30, 40 };
 
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("SHA-256");
 
            // display the digest value before Updation
            System.out.println("MessageDigest before update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
 
            // update MessageDigest value
            // using update() method
            msd1.update(bb);
 
            // display the digest value before Updation
            System.out.println("\nMessageDigest after update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // reset MessageDigest value
            // using reset() method
            msd1.reset();
 
            // display the digest value after reset
            System.out.println("\nMessageDigest after reset : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
MessageDigest before update : -11235

MessageDigest after update : 30835

MessageDigest after reset : -11235

示例 2:

Java

// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.nio.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            byte[] barr = { 10, 20, 30, 40 };
 
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("SHA-256");
 
            // display the digest value before Updation
            System.out.println("MessageDigest before update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
 
            // update MessageDigest value
            // using update() method
            msd1.update(bb);
 
            // display the digest value before Updation
            System.out.println("\nMessageDigest after update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // reset MessageDigest value
            // using reset() method
            msd1.reset();
 
            // display the digest value after reset
            System.out.println("\nMessageDigest after reset : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
MessageDigest before update : -7248

MessageDigest after update : 24403

MessageDigest after reset : -7248

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