Java MD5 String md5(File file)

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

Description

md

License

Apache License

Declaration

public static String md5(File file) throws NoSuchAlgorithmException, IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5(File file) throws NoSuchAlgorithmException, IOException {
        //        try {
        //            String s = PULSAR_CAUSTIC.getAbsolutePath();
        //            MessageDigest md5 = MessageDigest.getInstance("MD5");
        //            md5.update(s.getBytes(), 0, s.length());
        //            String signature = new BigInteger(1, md5.digest()).toString(16);
        //            System.out.println("Signature: " + signature);
        ///*from  w w  w.  j  av a 2s .  co  m*/
        //        } catch (final NoSuchAlgorithmException e) {
        //            e.printStackTrace();
        //        }
        MessageDigest md = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(file);

        byte[] dataBytes = new byte[1024];

        int nread = 0;
        while ((nread = fis.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }

        fis.close();

        byte[] mdbytes = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        //System.out.println("Digest(in hex format):: " + sb.toString());
        //assertEquals("8ff156ff36376d2517cef39cdd43876a", sb.toString());
        return sb.toString();
    }
}

Related

  1. md5(File f)
  2. md5(File f)
  3. md5(File f)
  4. md5(File file)
  5. md5(File file)
  6. md5(File gcdZipFile)
  7. md5(final File file)
  8. md5(final InputStream in)
  9. md5(final String data)