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) throws Exception 

Method Source Code

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

import java.security.*;

public class Main {
    public static String md5(String text) throws Exception {
        return md5(text.getBytes("UTF-8"));
    }//  w w  w.  j  a v  a 2 s .  c om

    public static String md5(byte[] source) throws Exception {
        int bufferSize = 4096;
        byte[] buffer = new byte[4096];

        MessageDigest md5 = MessageDigest.getInstance("MD5");

        int remain = source.length;

        while (remain > 0) {
            int len = (remain > bufferSize) ? bufferSize : remain;
            System.arraycopy(source, source.length - remain, buffer, 0, len);
            remain = remain - len;

            md5.update(buffer, 0, len);
        }

        return byte2Hex(md5.digest());
    }

    public static String byte2Hex(byte[] bytes) throws Exception {
        final String HEX = "0123456789abcdef";

        String result = "";
        for (int i = 0; i < bytes.length; i++) {
            result += HEX.charAt(bytes[i] >> 4 & 0x0F);
            result += HEX.charAt(bytes[i] & 0x0F);
        }

        return new String(result);
    }
}

Related

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