Java MD5 Sum md5sum(InputStream file)

Here you can find the source of md5sum(InputStream file)

Description

mdsum

License

Open Source License

Declaration

public static String md5sum(InputStream file) 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.math.BigInteger;

import java.security.MessageDigest;

public class Main {
    public static String md5sum(InputStream file) {
        InputStream in = file;/*from   www  . j ava  2 s .c o  m*/

        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[8192];
            int read = 0;

            while ((read = in.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }

            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            return bigInt.toString(16);
        } catch (Exception e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
                }
        }
    }
}

Related

  1. md5sum(File f)
  2. md5sum(File f, char[] cbuf, MessageDigest digest, byte[] bbuf)
  3. md5sum(File file)
  4. md5sum(File file)
  5. md5sum(File library)
  6. md5sum(InputStream in)
  7. md5sum(String data)
  8. md5sum(String input)
  9. md5sum(String inString)