MD5.java Source code

Java tutorial

Introduction

Here is the source code for MD5.java

Source

import javax.swing.JOptionPane;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5 common utils. Check and calculate MD5 BASE64 sum.
 * 
 * @author Luigi Zurolo - Liscio Luca
 * @version 1.1
 */
public class MD5 {

    /**
     * MD5 BASE64 checksum for the specified input string.
     * 
     * @param input -
     *          Specified input string
     * @return String - MD5 BASE64 sum
     */
    public static String checkMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte[] enc = md.digest();
            String md5Sum = new sun.misc.BASE64Encoder().encode(enc);
            return md5Sum;
        } catch (NoSuchAlgorithmException nsae) {
            System.out.println(nsae.getMessage());
            return null;
        }
    }
}