Java MD5 digestMD5(String data)

Here you can find the source of digestMD5(String data)

Description

Digests the data with MD5 and returns the raw unencoded bytes.

License

Apache License

Declaration

public static String digestMD5(String data) 

Method Source Code


//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

public class Main {
    /**/* w ww .  ja  v a2 s . co  m*/
     * "MD5"
     */
    public static final String MD5 = "MD5";
    /**
     * The hex chars as bytes.
     */
    public static final byte[] HEXADECIMAL = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61,
            0x62, 0x63, 0x64, 0x65, 0x66 };

    /**
     * Digests the {@code data} with MD5 and returns the raw unencoded bytes.
     */
    public static String digestMD5(String data) {
        return getDigestedValue(MD5, data.getBytes());
    }

    /**
     * Digests the {@code data} with MD5 and the specified {@code charset} and 
     * returns the raw unencoded bytes.
     */
    public static String digestMD5(String data, String charset) {
        try {
            return getDigestedValue(MD5, data, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Digests the {@code data} with the specified {@code type} and {@code charset}.
     */
    public static String getDigestedValue(String type, String data, String charset)
            throws UnsupportedEncodingException {
        return getDigestedValue(type, data.getBytes(charset), charset);
    }

    /**
     * Digests the {@code data} with the specified {@code type}.
     */
    public static String getDigestedValue(String type, String data) {
        return getDigestedValue(type, data.getBytes());
    }

    static String getDigestedValue(String type, byte[] data, String charset) {
        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data), charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static String getDigestedValue(String type, byte[] data) {
        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static String getHexString(byte[] data) {
        return new String(getHexBytes(data));
    }

    /**
     * Returns the bytes {@code data} in hex form as string.
     */
    public static String getHexString(byte[] data, String charset) throws UnsupportedEncodingException {
        return new String(getHexBytes(data), charset);
    }

    /**
     * Returns the bytes {@code data} in hex form.
     */
    public static byte[] getHexBytes(byte[] data) {

        byte[] out = new byte[data.length * 2];
        for (int i = 0, y = 0; i < data.length; i++) {
            int l = (data[i] & 0xf0) >> 4;
            int r = data[i] & 0x0f;
            out[y++] = HEXADECIMAL[l];
            out[y++] = HEXADECIMAL[r];
        }
        return out;
    }
}

Related

  1. decodeMd5(String source)
  2. decryMd5(String source)
  3. digestMD5(byte[] data)
  4. digestMd5(final String value)
  5. digestMD5(String buffer, String key)
  6. digestMD5(String login, String pass)
  7. digestMd5(String plain)
  8. digestMD5(String text)
  9. encryptmd5(String str)