Java MD5 Sum md5sum(byte[] b)

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

Description

mdsum

License

Apache License

Declaration

public static String md5sum(byte[] b) 

Method Source Code


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

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

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

    public static String md5sum(byte[] b) {
        try {/*from w  ww. j av a2  s.  c  o m*/
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(b, 0, b.length);
            return toHexString(md5.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(hexChar[((b[i] & 0xF0) >>> 4)]);
            sb.append(hexChar[(b[i] & 0xF)]);
        }
        return sb.toString();
    }
}

Related

  1. md5sum(byte[] content)
  2. md5Sum(byte[] data)
  3. md5sum(byte[] input, int length)
  4. MD5Sum(byte[] par1Data)