Java MD5 String MD5(String text)

Here you can find the source of MD5(String text)

Description

MD

License

Open Source License

Declaration

public static String MD5(String text) 

Method Source Code

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

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

public class Main {
    private static final String MD5 = "MD5";
    private static final String ISO_8859_1 = "iso-8859-1";

    public static String MD5(String text) {

        MessageDigest md;/*from w  w w.j a v  a2s.co  m*/

        try {
            md = MessageDigest.getInstance(MD5);
            byte[] md5hash = new byte[32];
            md.update(text.getBytes(ISO_8859_1), 0, text.length());
            md5hash = md.digest();
            return convertToHex(md5hash);
        } catch (NoSuchAlgorithmException e1) {
            System.err.println(e1);

        } catch (UnsupportedEncodingException e2) {
            System.err.println(e2);
        }

        return null;
    }

    public static String MD5(byte[] bytes) {

        MessageDigest md;

        try {
            md = MessageDigest.getInstance(MD5);
            byte[] md5hash = md.digest(bytes);
            return convertToHex(md5hash);
        } catch (NoSuchAlgorithmException e1) {
            System.err.println(e1);
        }
        return null;
    }

    public static String convertToHex(byte[] data) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }
}

Related

  1. md5(String string)
  2. md5(String string)
  3. md5(String string)
  4. md5(String strs)
  5. md5(String target)
  6. md5(String text)
  7. MD5(String text)
  8. md5(String text)
  9. md5(String text)