Java MD5 Byte Array md5(byte[] data)

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

Description

Make MD5 diaguest.

License

Open Source License

Declaration

public static String md5(byte[] data) 

Method Source Code

//package com.java2s;

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

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

    /**/*  w  w  w .ja  v a 2 s  . c om*/
     * Make MD5 diaguest.
     */
    public static String md5(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buf = md.digest(data);
            return toHexString(buf);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 Algorithm not supported", e);
        }
    }

    public static String toHexString(byte[] bytes) {
        int length = bytes.length;
        StringBuffer sb = new StringBuffer(length * 2);
        int x = 0;
        int n1 = 0, n2 = 0;
        for (int i = 0; i < length; i++) {
            if (bytes[i] >= 0)
                x = bytes[i];
            else
                x = 256 + bytes[i];
            n1 = x >> 4;
            n2 = x & 0x0f;
            sb = sb.append(HEX[n1]);
            sb = sb.append(HEX[n2]);
        }
        return sb.toString();
    }
}

Related

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