How to get File Checksum using MD5?

MessageDigest md = MessageDigest.getInstance( "MD5" );
FileInputStream fis = new FileInputStream( fileName ); // D:/export.txt
BufferedInputStream bufferedInputStream = new BufferedInputStream( fis );
byte[] dataBytes = new byte[8 * 1024];
int nread = 0;
while ( ( nread = bufferedInputStream.read( dataBytes ) ) != -1 ) {
	md.update( dataBytes, 0, nread );
}
;
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < mdbytes.length; i++ ) {
	sb.append( Integer.toString( ( mdbytes[ i ] & 0xff ) + 0x100, 32 ) 
		.substring( 1 ) );
}
bufferedInputStream.close();
fis.close();
System.out.println( "Digest(in hex format):: " + sb.toString() );
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: