Java MD5 Byte Array md5(byte[] source)

Here you can find the source of md5(byte[] source)

Description

md

License

Apache License

Declaration

public static String md5(byte[] 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 final String ALGORITHM = "MD5";
    private static final String DEFAULT_CHARSET = "UTF-8";
    private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    public static String md5(byte[] source) {
        try {/*  w w w .  j  ava2 s .c  om*/
            MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
            messageDigest.update(source);
            byte[] digest = messageDigest.digest();

            int j = digest.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = digest[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String md5(String text) {
        return md5(text, DEFAULT_CHARSET);
    }

    public static String md5(String str, String charset) {
        try {
            return md5(str.getBytes(charset));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

  1. md5(byte[] input)
  2. md5(byte[] input)
  3. md5(byte[] input)
  4. md5(byte[] md5)
  5. md5(byte[] param)
  6. MD5(byte[] source)
  7. md5(byte[] source)
  8. md5(byte[] source)
  9. MD5(byte[] src)