Java MD5 Sum md5sum(String inString)

Here you can find the source of md5sum(String inString)

Description

Calculate the MD5 checksum of the input string

License

Open Source License

Parameter

Parameter Description
inString Input string

Return

MD5 checksum of the input string in hexadecimal value

Declaration

public static String md5sum(String inString) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from w  w  w  . j a  v a2  s .c om
     * Calculate the MD5 checksum of the input string
     * @param inString Input string
     * @return MD5 checksum of the input string in hexadecimal value
     */
    public static String md5sum(String inString) {
        MessageDigest algorithm = null;

        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            System.err.println("Cannot find digest algorithm");
            return null;
        }

        byte[] defaultBytes = inString.getBytes();
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        return hexToString(messageDigest);
    }

    /**
     * Converts a byte array to string.
     * 
     * @param data
     *            Input byte array.
     * @return String
     */
    public static String hexToString(byte[] data) {
        if (data == null) {
            return "";
        }
        ;
        StringBuffer sb = new StringBuffer(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(0xFF & data[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString().toUpperCase();
    }

    /**
     * Converts object to a string.<br>
     * If object is null then return null
     * 
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        if (null == obj) {
            return null;
        } else {
            return obj.toString();
        }
    }
}

Related

  1. md5sum(File library)
  2. md5sum(InputStream file)
  3. md5sum(InputStream in)
  4. md5sum(String data)
  5. md5sum(String input)
  6. md5sum(String message)
  7. md5Sum(String msg)
  8. md5sum(String str)
  9. md5sum(String string)