Java MD5 String md5Digest(final InputStream data)

Here you can find the source of md5Digest(final InputStream data)

Description

md Digest

License

Apache License

Declaration

public static byte[] md5Digest(final InputStream data) throws NoSuchAlgorithmException, IOException 

Method Source Code


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

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final int STREAM_BUFFER_LENGTH = 1024;

    public static byte[] md5Digest(final InputStream data) throws NoSuchAlgorithmException, IOException {
        MessageDigest digest = getMD5MessageDigest();
        updateMessageDigest(digest, data);
        return digest.digest();
    }// w  w w . j  a  v a  2  s  .co  m

    public static byte[] md5Digest(final byte[] data) throws NoSuchAlgorithmException {
        return getMD5MessageDigest().digest(data);
    }

    public static byte[] md5Digest(final String data, final Charset charset) throws NoSuchAlgorithmException {
        return getMD5MessageDigest().digest(data.getBytes(charset));
    }

    public static MessageDigest getMD5MessageDigest() throws NoSuchAlgorithmException {
        return getMessageDigest("MD5");
    }

    private static void updateMessageDigest(MessageDigest digest, final InputStream data) throws IOException {
        final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
        int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);

        while (read > -1) {
            digest.update(buffer, 0, read);
            read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
        }
    }

    public static MessageDigest getMessageDigest(final String algorithm) throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(algorithm);
    }
}

Related

  1. MD5_HEX(String source)
  2. md5AsHexString(String text, String charset)
  3. md5Base64(String str)
  4. md5ByHex(String src)
  5. md5crypt(String s)
  6. md5Digest(final String message)
  7. md5digest(String key)
  8. md5Digest(String s)
  9. md5Digest(String text)