MD5 string : MD5 « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Security » MD5Screenshots 
MD5 string
 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
  private static final String TAG = "MD5";

  public static boolean checkMD5(String md5, File updateFile) {
    if (md5 == null || md5.equals(""|| updateFile == null) {
      return false;
    }

    String calculatedDigest = calculateMD5(updateFile);

    if (calculatedDigest == null) {
      return false;
    }
    return calculatedDigest.equalsIgnoreCase(md5);
  }

  public static String calculateMD5(File updateFile) {
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("MD5");
    catch (NoSuchAlgorithmException e) {
      return null;
    }
    InputStream is;
    try {
      is = new FileInputStream(updateFile);
    catch (FileNotFoundException e) {
      return null;
    }
    byte[] buffer = new byte[8192];
    int read;
    try {
      while ((read = is.read(buffer)) 0) {
        digest.update(buffer, 0, read);
      }
      byte[] md5sum = digest.digest();
      BigInteger bigInt = new BigInteger(1, md5sum);
      String output = bigInt.toString(16);
      // Fill to 32 chars
      output = String.format("%32s", output).replace(' ''0');
      return output;
    catch (IOException e) {
      throw new RuntimeException("Unable to process file for MD5", e);
    finally {
      try {
        is.close();
      catch (IOException e) {
        System.out.println(e);
      }
    }
  }

  public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {
      Process p = Runtime.getRuntime().exec("su");
      OutputStream os = p.getOutputStream();
      os.write(("md5sum " + recoveryFilename).getBytes());
      os.flush();
      os.close();
      InputStream is = p.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      String str = br.readLine();
      MD5string = str.split("  ")[0].trim();
      is.close();
      br.close();
      p.destroy();
    catch (Exception e) {
      System.out.println(e);
      return null;
    }
    System.out.println(MD5string);
    return MD5string;
  }
}

   
  
Related examples in the same category
1.OTP one-time password calculationOTP one-time password calculation
2.Applet to serve as an s/key calculator application wrapper around otp class
3.Creating a Keyed Digest Using MD5
4.MD5 BASE64 checksum for the specified input string.
5.MD5 InputStream
6.Implements MD5 functionality on a stream.
7.Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher
8.MD5 algorithm RFC 1321
9.Contains internal state of the MD5 class
10.Create MD5 String
11.MD5 Sum
12.MD5 Hashing utility that builds up a hash from a series of objects and base types fed into it.
13.MD5 MessageDigest
14.Get MD5 string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.