Java MD5 String md5Hex(String data)

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

Description

md Hex

License

Apache License

Declaration

public static String md5Hex(String data) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String md5Hex(String data) {
        if (data == null) {
            throw new IllegalArgumentException("data must not be null");
        }//w w  w . j  a  va2  s .com

        byte[] bytes = digest("MD5", data);

        return toHexString(bytes);
    }

    private static byte[] digest(String algorithm, String data) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        return digest.digest(data.getBytes());
    }

    private static String toHexString(byte[] bytes) {
        int l = bytes.length;

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

        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & bytes[i]) >>> 4];
            out[j++] = DIGITS[0x0F & bytes[i]];
        }

        return new String(out);
    }
}

Related

  1. md5Encryption(String str)
  2. md5File(File f)
  3. md5file(File file)
  4. md5FromFile(File file)
  5. md5fromFile(String path)
  6. md5Hex(String data)
  7. md5Hex(String message)
  8. md5Hex(String message)
  9. md5Hex(String message)