Java MD5 Byte Array md5Hex(byte[] data)

Here you can find the source of md5Hex(byte[] data)

Description

Calculates the MD5 digest and returns the symbol as a 32 character hex string.

License

Open Source License

Parameter

Parameter Description
data Data to digest

Return

MD5 digest as a hex string

Declaration

public static String md5Hex(byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static final String MD5_ALGORITHM = "MD5";
    /**//  ww w  .java 2 s . co m
     * Used building output as Hex
     */
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * Calculates the MD5 digest and returns the symbol as a 32 character
     * hex string.
     *
     * @param data Data to digest
     * @return MD5 digest as a hex string
     */
    public static String md5Hex(byte[] data) {
        return new String(encodeHex(md5(data)));
    }

    /**
     * Calculates the MD5 digest and returns the symbol as a 32 character
     * hex string.
     *
     * @param data Data to digest
     * @return MD5 digest as a hex string
     */
    public static String md5Hex(String data) {
        return new String(encodeHex(md5(data)));
    }

    /**
     * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
     * The returned array will be double the length of the passed array, as it takes two characters to represent any
     * given byte.
     *
     * @param data
     *                  a byte[] to convert to Hex characters
     * @return A char[] containing hexidecimal characters
     */
    public static char[] encodeHex(byte[] data) {

        int l = data.length;

        char[] out = new char[l << 1];

        // two characters form the hex symbol.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }

        return out;
    }

    /**
     * Calculates the MD5 digest and returns the symbol as a 16 element
     * <code>byte[]</code>.
     *
     * @param data Data to digest
     * @return MD5 digest
     */
    public static byte[] md5(byte[] data) {
        return getMD5().digest(data);
    }

    /**
     * Calculates the MD5 digest and returns the symbol as a 16 element
     * <code>byte[]</code>.
     *
     * @param data Data to digest
     * @return MD5 digest
     */
    public static byte[] md5(String data) {
        return md5(data.getBytes());
    }

    /**
     * Returns an MD5 MessageDigest.
     *
     * @return An MD5 digest creating.
     * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
     */
    private static MessageDigest getMD5() {
        return getMessageDigest(MD5_ALGORITHM);
    }

    public static MessageDigest getMessageDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}

Related

  1. MD5Bytes(byte[] src)
  2. md5Bytes(String message)
  3. md5Bytes(String s)
  4. md5Digest(byte[] bytes)
  5. md5Hex(byte data[])
  6. md5key(byte[] key)
  7. md5P(final ByteBuffer bb)
  8. MD5Raw(byte[] src)