Java MD5 String md5(String path_of_file_)

Here you can find the source of md5(String path_of_file_)

Description

md

License

Open Source License

Declaration

public static String md5(String path_of_file_) 

Method Source Code

//package com.java2s;
/*/*  w  ww. j a  v  a2 s . com*/
 * Copyright (C) 2010-2014 Laurent CLOUET
 * Author Laurent CLOUET <laurent.clouet@nopnop.net>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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

import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5(String path_of_file_) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            InputStream is = Files.newInputStream(Paths.get(path_of_file_));
            DigestInputStream dis = new DigestInputStream(is, md);
            byte[] buffer = new byte[8192];
            while (dis.read(buffer) > 0)
                ; // process the entire file
            BigInteger bigInt = new BigInteger(1, md.digest());
            String data = bigInt.toString(16).toLowerCase();
            if ((data.length() % 2) != 0) {
                data = "0" + data;
            }
            dis.close();
            is.close();
            return data;
        } catch (NoSuchAlgorithmException | IOException e) {
            return "";
        }
    }
}

Related

  1. md5(String orgin)
  2. md5(String pass)
  3. md5(String password)
  4. md5(String password)
  5. md5(String password)
  6. MD5(String pData)
  7. md5(String plain)
  8. md5(String plainText)
  9. md5(String plainText)