javamd5chunkingmessage-digesttext-chunking

How to use update Method of Message Digest Class


I'm working on a data encryption project and just wanted to ask how to use the method update of message digest class. In a code snippet of MD5 implementation, this was written.

import java.security.MessageDigest;
import java.util.*;

class MD5{
    public static void main(String[]args){
        Scanner cin=new Scanner(System.in);
        String s=cin.nextLine();
        try{
            MessageDigest md=MessageDigest.getInstance("MD5");
            byte[] dataBytes=s.getBytes(); 
            md.update(dataBytes,0,0);
            byte[] digest=md.digest();
            for(byte b:digest)System.out.printf("%02x",b);
        }catch(Exception e){}
    }
}

and i'm confused about this line

md.update(dataBytes,0,0);

what are the three arguments are used for? And how to hash only a certain number say 192 bytes of a data.


Solution

  • This is what i get from this, and this

    what are the three arguments are used for?

    update(byte[] input, int offset, int len)
    

    Updates the digest using the specified array of bytes, starting at the specified offset.

    input is the array which is supposed to be hashed

    offset is the index of the array which is the starting point

    len specifies how far from the starting index should it go