Cleanup Utils.md5 method by using DatatypeConverter and DigestInputStream classes

This commit is contained in:
Mathis Chenuet
2015-03-05 20:27:13 +00:00
committed by Laurent Clouet
parent a86b6c2754
commit 640926110b

View File

@@ -21,11 +21,12 @@ package com.sheepit.client;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Calendar; import java.util.Calendar;
@@ -36,6 +37,8 @@ import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import javax.xml.bind.DatatypeConverter;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@@ -102,55 +105,19 @@ public class Utils {
} }
public static String md5(String path_of_file_) { public static String md5(String path_of_file_) {
MessageDigest digest;
try { try {
digest = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
} InputStream is = Files.newInputStream(Paths.get(path_of_file_));
catch (NoSuchAlgorithmException e1) { DigestInputStream dis = new DigestInputStream(is, md);
e1.printStackTrace();
return "";
}
File f = new File(path_of_file_);
InputStream is;
try {
is = new FileInputStream(f);
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
return "";
}
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int read = 0; while (dis.read(buffer) > 0)
try { ; // process the entire file
while ((read = is.read(buffer)) > 0) { return DatatypeConverter.printHexBinary(md.digest()).toLowerCase();
digest.update(buffer, 0, read);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);
// fill with "0" because bigInt.toString does not add 0 at the beginning of the result
int zero_to_add = 32 - output.length();
for (int i = 0; i < zero_to_add; i++)
output = "0" + output;
return output;
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
}
catch (IOException e) {
e.printStackTrace();
//throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
}
} }
catch (NoSuchAlgorithmException | IOException e) {
return ""; return "";
} }
}
public static double lastModificationTime(File directory_) { public static double lastModificationTime(File directory_) {
double max = 0.0; double max = 0.0;