• Home
  • About
  • My Apps
    • AnReboot
    • BlitzDroid
    • SavePoint
    • Javelt
    • TagihanListrik
    • TagihanTelkom
    • CekTagihan
    • Komodonesia
    • Indonesia FlightBoard
    • Indonesia Soccer
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

LORENZ BLOG

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> Sample Code to Get MD5 Checksum of a File in Android

Sample Code to Get MD5 Checksum of a File in Android

September 11, 2012 by Lorensius Londa 4 Comments

When downloading a file over internet, the uploader usually provides an MD5 checksum for the file to ensure the integrity of the file. MD5 checksum (message digest) is something like a fingerprint or digital signature for  a file. The checksum is unique for a file and  there is very small possibility of getting two identical checksums for two different files. In Android, using java security package we can compute the md5 message digest for a file. Here is a sample class to get MD5 checksum from an input stream:

package net.londatiga.android.util;

import java.security.MessageDigest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Util {
	private static char[] hexDigits = "0123456789abcdef".toCharArray();

	public static String md5(InputStream is) throws IOException {
		String md5 = "";

		try {
		    byte[] bytes = new byte[4096];
		    int read = 0;
		    MessageDigest digest = MessageDigest.getInstance("MD5");

		    while ((read = is.read(bytes)) != -1) {
		        digest.update(bytes, 0, read);
		    }

		    byte[] messageDigest = digest.digest();

		    StringBuilder sb = new StringBuilder(32);

		    for (byte b : messageDigest) {
		        sb.append(hexDigits[(b >> 4) & 0x0f]);
		        sb.append(hexDigits[b & 0x0f]);
		    }

		    md5 = sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}

	    return md5;
	}
}

To get md5 checksum from a file and compare it with original md5:

try {
      String md5Origin	= "";//original file's md5 checksum
	  String filePath   = ""; //fill with the real file path name

	  FileInputStream fis   = new FileInputStream(filePath);
	  String md5Checksum	= Util.md5(fis);

	  if (md5Checksum.equals(md5Origin)) {
		  //file is valid
	  }
} catch (Exception e) {
}
Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Related posts:

  1. Blue Bamboo P25 Printer Android Demo Application With Source Code
  2. Simple JSON RPC Client for Android
  3. How to Send Image to Twitpic from Android
  4. How to Make Android ListView or GridView Expandable inside ScrollView

Filed Under: Android, Programming Tagged With: Android, md5, md5 checksum, message digest

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Comments

  1. masashi says

    March 1, 2014 at 3:40 am

    This is useful. Thanks!

    Reply
  2. desmond says

    September 23, 2014 at 7:24 am

    i want the md5 file

    Reply
  3. desmond says

    September 23, 2014 at 7:25 am

    fast

    Reply
  4. Dmitri says

    October 29, 2014 at 8:07 pm

    This works, great!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

 

About Me

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania. Read More…

  • Facebook
  • Github
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

How to Use Multi-User Feature in Android Jelly Bean

Recent Comments

  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices
  • Kiran Ahuja on How to Programmatically Pair or Unpair Android Bluetooth Device
  • Edwin Alvaradko on How to Make Android Map Scrollable Inside a ScrollView Layout

Recent Posts

  • Eclipse Issue on Os X Yosemite – To open “Eclipse” you need to install the legacy Java SE 6 runtime
  • Blue Bamboo P25 Printer Android Demo Application With Source Code
  • How to Programmatically Pair or Unpair Android Bluetooth Device
  • How to Programmatically Scan or Discover Android Bluetooth Devices

Latest Tweets

  • I just finished running 12.89 km in 1h:23m:28s with #Endomondo #endorphins https://t.co/hYgBSfmGxSabout 1 hour ago
  • I just finished walking 1.77 km in 22m:40s with #Endomondo #endorphins https://t.co/AMKDdnwTq02 days ago

Copyright © 2019 · Londatiga.Net . All Rights Reserved