Compute the md5 hash of a String. - Java Security

Java examples for Security:MD5

Description

Compute the md5 hash of a String.

Demo Code


//package com.java2s;

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static void main(String[] argv) {
        String arg = "java2s.com";
        System.out.println(computeMd5OfString(arg));
    }/*from   ww  w .j  ava 2  s  .  c o m*/

    /**
     * Lower case Hex Digits.
     */
    private static final String HEX_DIGITS = "0123456789abcdef";
    /**
     * Byte mask.
     */
    private static final int BYTE_MSK = 0xFF;
    /**
     * Hex digit mask.
     */
    private static final int HEX_DIGIT_MASK = 0xF;
    /**
     * Number of bits per Hex digit (4).
     */
    private static final int HEX_DIGIT_BITS = 4;

    /**
     * Compute the md5 hash of a String. using the RSA Data Security, Inc.
     * MD5 Message-Digest Algorithm, and returns that hash. Throws
     * UnsupportedOperationException in case of errors.
     *
     * @param arg
     * the UTF-8 String to be encoded
     * @return a 32-character hexadecimal number
     * @throws UnsupportedOperationException
     * in the case the VM doesn't support UTF-8 or MD5, you
     * shouldn't bother catching this exception
     */
    public static String computeMd5OfString(final String arg)
            throws UnsupportedOperationException {
        try {
            return computeMd5OfByteArray(arg.getBytes(("UTF-8")));
        } catch (UnsupportedEncodingException ex) {
            throw new UnsupportedOperationException(ex);
        }
    }

    /**
     * Compute the md5 hash of a byte array.
     *
     * @param arg
     * the raw byte array to be encoded
     * @return a 32-character hexadecimal number
     * @throws UnsupportedOperationException
     * in the case MD5 MessageDigest is not supported, you
     * shouldn't bother catching this exception
     */
    public static String computeMd5OfByteArray(final byte[] arg)
            throws UnsupportedOperationException {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(arg);
            byte[] res = md.digest();
            return toHexString(res);
        } catch (NoSuchAlgorithmException ex) {
            throw new UnsupportedOperationException(ex);
        }
    }

    /**
     * Compute a String in HexDigit from the input.
     *
     * @param byteArray
     * a row byte array
     * @return a hex String
     */
    private static String toHexString(final byte[] byteArray) {
        StringBuilder sb = new StringBuilder(byteArray.length * 2);
        for (int i = 0; i < byteArray.length; i++) {
            int b = byteArray[i] & BYTE_MSK;
            sb.append(HEX_DIGITS.charAt(b >>> HEX_DIGIT_BITS)).append(
                    HEX_DIGITS.charAt(b & HEX_DIGIT_MASK));
        }
        return sb.toString();
    }
}

Related Tutorials