Java MD5 Byte Array MD5(byte[] src)

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

Description

MD

License

Open Source License

Declaration

public static String MD5(byte[] src) 

Method Source Code

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

import java.io.*;

import java.security.*;

public class Main {
    private static final ThreadLocal<MessageDigest> md5 = new ThreadLocal<MessageDigest>() {
        @Override/*w  ww  .j  a v a  2  s  .c o m*/
        protected MessageDigest initialValue() {
            MessageDigest crypt = null;
            try {
                crypt = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return crypt;
        }
    };

    public static String MD5(byte[] src) {
        try {
            MessageDigest crypt = MessageDigest.getInstance("MD5");
            crypt.reset();
            crypt.update(src);
            return ToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String MD5(RandomAccessFile randomAccessFile) {
        try {
            MessageDigest crypt = md5.get();
            crypt.reset();
            byte[] block = new byte[8 * 1024];
            for (int i = 0; i < randomAccessFile.length(); i += 8 * 1024) {
                int len = (int) Math.min(block.length, randomAccessFile.length() - i);
                randomAccessFile.readFully(block, 0, len);
                crypt.update(block, 0, len);
            }
            return ToHex(crypt.digest());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String ToHex(byte[] src) {
        String res = "";
        for (int i = 0; i < src.length; i++) {
            res += String.format("%02X", src[i] & 0xFF);
        }
        return res.toLowerCase();
    }
}

Related

  1. md5(byte[] param)
  2. MD5(byte[] source)
  3. md5(byte[] source)
  4. md5(byte[] source)
  5. md5(byte[] source)
  6. md5(byte[] str)
  7. md5(final byte[] b)
  8. md5(final byte[] bytes)
  9. md5(final byte[] data)