Java MD5 String md5(String source)

Here you can find the source of md5(String source)

Description

md

License

Apache License

Declaration

public static String md5(String source) 

Method Source Code


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

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

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

    public static String md5(String source) {
        try {//from  w w w  .  j  a va2  s .  com
            byte[] bytes = md5(source.getBytes("utf-8"));
            char str[] = new char[bytes.length * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte byte0 = bytes[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] md5(byte[] source) {
        byte[] result = new byte[0];
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source);
            result = md.digest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. md5(String s)
  2. md5(String s)
  3. md5(String s)
  4. md5(String s)
  5. md5(String sInput)
  6. md5(String source)
  7. md5(String source)
  8. md5(String source)
  9. md5(String source)