Java MD5 Sum MD5Sum(byte[] par1Data)

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

Description

MD Sum

License

Open Source License

Declaration

public static String MD5Sum(byte[] par1Data) 

Method Source Code

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

import java.io.FileInputStream;

import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String MD5Sum(byte[] par1Data) {
        try {/*from  w  ww . ja va  2s. co  m*/
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            StringBuilder sb = new StringBuilder();
            for (byte t : md5.digest(par1Data)) {
                sb.append(Integer.toHexString(t & 0xFF));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String MD5Sum(String par1Data) {
        return MD5Sum(par1Data.getBytes());
    }

    public static String MD5Sum(FileInputStream par1Stream) {
        FileChannel ic = null;
        ByteBuffer bb = null;
        try {
            ic = par1Stream.getChannel();
            bb = ByteBuffer.allocate((int) ic.size());
            ic.read(bb);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (ic != null)
                    ic.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return MD5Sum(bb.array());
    }
}

Related

  1. md5sum(byte[] b)
  2. md5sum(byte[] content)
  3. md5Sum(byte[] data)
  4. md5sum(byte[] input, int length)
  5. md5sum(File f)
  6. md5sum(File f, char[] cbuf, MessageDigest digest, byte[] bbuf)
  7. md5sum(File file)
  8. md5sum(File file)