• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • 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) {
}
Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

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

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.

Reader Interactions

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 *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

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

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 310 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg314 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in