Java MD5 getMD5(File file)

Here you can find the source of getMD5(File file)

Description

Get the MD5 Hash from the given File.

License

Open Source License

Parameter

Parameter Description
file the File to create Hash from.

Exception

Parameter Description
Exception an exception

Return

the Hash as Hex String.

Declaration

public static String getMD5(File file) throws Exception 

Method Source Code


//package com.java2s;
/*// w  ww.jav a 2 s .  c  om
 * Copyright (c) 2015 "JackWhite20"
 *
 * This file is part of PatchyAPI.
 *
 * PatchyAPI 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

import javax.xml.bind.DatatypeConverter;
import java.io.File;

import java.nio.file.Files;

import java.nio.file.Paths;
import java.security.MessageDigest;

public class Main {
    /**
     * Get the MD5 Hash from the given File.
     *
     * @param file the File to create Hash from.
     * @return the Hash as Hex String.
     * @throws Exception
     */
    public static String getMD5(File file) throws Exception {
        String path = file.getAbsolutePath();
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(Files.readAllBytes(Paths.get(path)));
        return DatatypeConverter.printHexBinary(md.digest()).toUpperCase();
    }
}

Related

  1. digestMD5(String text)
  2. encryptmd5(String str)
  3. generateMD5ByContent(String content)
  4. getFileMD5(String filePath)
  5. getHexMd5(byte[] bytes)
  6. getMD5ByString(String orginalString)
  7. getMD5Checksum(String path)
  8. getMd5DigestHex(final String content)
  9. getMD5Hex(byte[] digest)