Java MD5 Byte Array md5(byte[] input)

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

Description

md

License

Apache License

Declaration

public static String md5(byte[] input) 

Method Source Code


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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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

    public static String md5(byte[] input) {
        if (input == null)
            return null;
        try {/* w w w  .j a  va2 s  .  c  o m*/
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(input);
            byte[] messageDigest = algorithm.digest();

            return byteArrayToHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    public static String byteArrayToHexString(byte[] byteArray) {
        String result = "";
        int len = byteArray.length;
        for (int i = 0; i < len; ++i) {
            byte b = byteArray[i];
            result = result + HEXDICT[(b >>> 4 & 0xF)];
            result = result + HEXDICT[(b & 0xF)];
        }
        return result;
    }
}

Related

  1. md5(byte[] data)
  2. md5(byte[] data)
  3. md5(byte[] data)
  4. md5(byte[] data)
  5. md5(byte[] data)
  6. md5(byte[] input)
  7. md5(byte[] input)
  8. md5(byte[] input)
  9. md5(byte[] md5)