Java

How to generate MD5 hash for your secret message3 min read

Passing messages is fun until someone notices you passing message in the class, but if you are using this hash nobody can read your message. The program will generate a hash you can only decrypt it only when you have a master encryption key.

MD5 (Message-Digest algorithm 5) is a widely used cryptographic hash function with a 128-bit hash value, specified in RFC 1321, MD5 is one in a series of message digest algorithms designed by Professor Ronald Rivest of MIT (Rivest, 1994). MD5 has been employed in a wide variety of security applications, and is also worldwide used to check the integrity of files.




MD5 is a perfect solution for security thinking. The Java developers usually may take over or code MD5 encryption, the MD5 encryption is complicated, for self-coding it’s hard to implement. Java standard edition has MD5 support built in. in package java.security, class MessageDigest supports functionality of a message digest algorithm, such as MD5 or SHA.

1. Example to Hash a String with MD5 algorithm

This is a Java MD5 example, it passes into a string and returns the MD5 encryption value, we used the method getBytes() that converts a plain text to byte array, the digest is then updated from the bytes from the byte array and a hash computation is conducted upon them(using MessageDigest).

2. Another way Hashing String with MD5 algorithm

This is another Java MD5 encryption example, it accepts string and returns MD5 encryption value.

3. File checksum using Java MD5

MD5 algorithm is also widely used for file checksum to ensure that files were not modified by attacker, an example is below that uses Java MD5 hashing algorithm to generate a checksum for file “c:\\apache\\cxf.jar”. you can check whether the printed value matches the original MD5 value which was released by Jar Publisher.

This Article is referred from the Oracle Documentation

Please take your time to comment on this article we feel responsible about what we write.

Leave a Comment