The MD5, defined in RFC 1321, is a hash algorithm to turn inputs into a fixed 128-bit (16 bytes) length of the hash value.
Note
MD5 is not collision-resistant – Two different inputs may producing the same hash value. Read this MD5 vulnerabilities. There are many fast and secure hashing algorithms like SHA3-256 or BLAKE2; For password hashing, we can use Bcrypt or Argon2. If possible, do not use MD5 in any security-related cryptography tasks.
In Java, we can use MessageDigest
to generate the MD5
algorithm.
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] result = md.digest(input);
1. Java MD5 Hashing
This Java example uses MD5
to produce a hash value from a String.
MD5Utils.java
package com.favtuts.crypto.hash; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Utils { private static final Charset UTF_8 = StandardCharsets.UTF_8; private static final String OUTPUT_FORMAT = "%-20s:%s"; private static byte[] digest(byte[] input) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } byte[] result = md.digest(input); return result; } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02x", b)); } return sb.toString(); } public static void main(String[] args) { String pText = "Hello MD5"; System.out.println(String.format(OUTPUT_FORMAT, "Input (string)", pText)); System.out.println(String.format(OUTPUT_FORMAT, "Input (length)", pText.length())); byte[] md5InBytes = MD5Utils.digest(pText.getBytes(UTF_8)); System.out.println(String.format(OUTPUT_FORMAT, "MD5 (hex) ", bytesToHex(md5InBytes))); // fixed length, 16 bytes, 128 bits. System.out.println(String.format(OUTPUT_FORMAT, "MD5 (length)", md5InBytes.length)); } }
Output
Input (string) :Hello MD5
Input (length) :9
MD5 (hex) :e5dadf6524624f79c3127e247f04b548
MD5 (length) :16
2. Java MD5 File Checksum.
For the file checksum, the ideas are the same, but we need some extra IO classes to handle the input stream.
Here is a text file.
c:\\test\\readme.txt
Hello MD5
This Java program will generate an MD5 file checksum from a file.
MD5Utils.java
package com.favtuts.crypto.hash; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Utils { private static final Charset UTF_8 = StandardCharsets.UTF_8; private static final String OUTPUT_FORMAT = "%-20s:%s"; private static byte[] checksum(String filePath) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } try (InputStream is = new FileInputStream(filePath); DigestInputStream dis = new DigestInputStream(is, md)) { while (dis.read() != -1) ; //empty loop to clear the data md = dis.getMessageDigest(); } catch (IOException e) { throw new IllegalArgumentException(e); } return md.digest(); } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02x", b)); } return sb.toString(); } public static void main(String[] args) { String file = "c:\\test\\readme.txt"; System.out.println(String.format(OUTPUT_FORMAT, "Input (file) ", file)); System.out.println(String.format(OUTPUT_FORMAT, "MD5 (checksum hex) ", bytesToHex(checksum(file)))); } }
Output
Input (file) :c:\test\readme.txt
MD5 (checksum hex) :e5dadf6524624f79c3127e247f04b548
3. Apache Commons Codec
This example uses the commons-codec
library to generate the MD5 hash value.
pom.xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
3.1 MD5 hash value from a String.
import org.apache.commons.codec.digest.DigestUtils;
String pText = "Hello MD5";
System.out.println(DigestUtils.md5Hex(password));
Output
e5dadf6524624f79c3127e247f04b548
3.2 MD5 hash value from a File.
try (InputStream is = new FileInputStream("c:\\test\\readme.txt")) {
String checksum = DigestUtils.md5Hex(is);
System.out.println(checksum);
} catch (IOException e) {
e.printStackTrace();
}
Output
e5dadf6524624f79c3127e247f04b548
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-crypto