diff --git a/core-java-modules/core-java-io-4/pom.xml b/core-java-modules/core-java-io-4/pom.xml index 9fc00ff586..2cc05698fc 100644 --- a/core-java-modules/core-java-io-4/pom.xml +++ b/core-java-modules/core-java-io-4/pom.xml @@ -32,6 +32,16 @@ log4j-over-slf4j ${org.slf4j.version} + + commons-codec + commons-codec + 1.15 + + + com.google.guava + guava + 31.1-jre + \ No newline at end of file diff --git a/core-java-modules/core-java-io-4/src/main/java/com/baeldung/md5checksum/Md5ChecksumGenerator.java b/core-java-modules/core-java-io-4/src/main/java/com/baeldung/md5checksum/Md5ChecksumGenerator.java new file mode 100644 index 0000000000..11c83591eb --- /dev/null +++ b/core-java-modules/core-java-io-4/src/main/java/com/baeldung/md5checksum/Md5ChecksumGenerator.java @@ -0,0 +1,44 @@ +package com.baeldung.md5checksum; + +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.google.common.io.ByteSource; +import org.apache.commons.codec.digest.DigestUtils; + +import java.io.File; +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.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Md5ChecksumGenerator { + + public static String genWithApacheCommons(String filePath) throws IOException { + try (InputStream is = Files.newInputStream(Paths.get(filePath))) { + return DigestUtils.md5Hex(is); + } + } + + public static String genWithGuava(String filePath) throws IOException { + File file = new File(filePath); + ByteSource byteSource = com.google.common.io.Files.asByteSource(file); + HashCode hc = byteSource.hash(Hashing.md5()); + return hc.toString(); + } + + public static String genWithMessageDigest(String filePath) throws IOException, NoSuchAlgorithmException { + byte[] data = Files.readAllBytes(Paths.get(filePath)); + byte[] hash = MessageDigest.getInstance("MD5").digest(data); + return new BigInteger(1, hash).toString(16); + } + + public static void main(String[] args) throws IOException, NoSuchAlgorithmException { + String filePath = "D:\\temp.txt"; + System.out.println(genWithApacheCommons(filePath)); + System.out.println(genWithMessageDigest(filePath)); + System.out.println(genWithGuava(filePath)); + } +}