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


Java MessageDigest reset()用法及代碼示例


java.security.MessageDigest類的reset()方法用於將當前消息摘要值重置為此MessageDisgest對象的默認消息摘要值。

用法:

public void reset()

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


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

示例1:

// 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() mehtod 
            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 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() mehtod 
            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–



相關用法


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