X

Java generate MD5 hash sum

In this example I will show you how to generate the MD5 hex sum for a given string using Java

Sometimes you may need to create the MD5 sum for a given string. This is often used to obfuscate passwords in the database or verify the content of a string. The MD5 hex sum can show you for example if a string has changed.



What is MD5

MD5 algorithm is a cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. Do not mistake hashing algorithms with encryption algorithms. The difference between hashing and encryption is that hashed strings can not be reverted back to original and encrypted content can be decrypted back to it’s original value.

Generate MD5 hashsum

Generate MD5 hashsum with Java

An easy way to generate the hashsum of a string is to use the static methods in Apache commons codec class DiagestUtils.

If you use Maven, put following dependency in your POM file:

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.2</version>
</dependency>

Or you can download the jar file from apache’s website: Apache commons codec

You can use following static method to hash your strings with MD5:

/**
* Returns the MD5 hex sum of given string
* @param str - the string to be hashed
* @return MD5 hex sum
*/
public static String md5(String str) {
	return DigestUtils.md5Hex(str);
}
0 0 votes
Article Rating
filip:
Related Post