Java MD5 Hash md5hash(byte[] input)

Here you can find the source of md5hash(byte[] input)

Description

mdhash

License

Apache License

Declaration

public static byte[] md5hash(byte[] input) 

Method Source Code


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

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static byte[] md5hash(byte[] input) {
        try {/*from w  ww . ja v  a2  s.c o m*/
            MessageDigest digest = MessageDigest.getInstance("MD5");
            return digest.digest(input);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Unknown algorithm: MD5");
        }
    }

    public static byte[] md5hash(File input) {
        InputStream in = null;
        byte[] buf = new byte[4096];
        int n;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            in = new BufferedInputStream(new FileInputStream(input));
            while ((n = in.read(buf, 0, buf.length)) != -1) {
                digest.update(buf, 0, n);
            }
            return digest.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Unknown algorithm: MD5");
        } catch (IOException e) {
            throw new RuntimeException(String.format("Can't read %s.", input), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignore) {
                }
            }
        }
    }
}

Related

  1. md5Hash(byte[] bytes)
  2. md5Hash(byte[] data)
  3. md5Hash(byte[] data)
  4. md5Hash(byte[] key)
  5. md5Hash(String algorithm, String input)
  6. md5Hash(String buf)
  7. md5Hash(String content)