0 votes
1 view
in Java by (1.3k points)

Is there any method to generate MD5 hash of a string in Java?

2 Answers

0 votes
by (32.7k points)

You can use MessageDigest class to generate an instance of MD5 hash:

 MessageDigest md = MessageDigest.getInstance("SHA-256");

 try {

     md.update(toChapter1);

     MessageDigest tc1 = md.clone();

     byte[] toChapter1Digest = tc1.digest();

     md.update(toChapter2);

     ...etc.

 } catch (CloneNotSupportedException cnse) {

     throw new DigestException("couldn't make digest of partial content");

 }

To know more about it click here.

0 votes
by (13.2k points)

You can use Class MessageDigest. It is hash functions which takes an arbitrary-sized data and the output is a fixed-length hash value.

This class contains a method getInstance which takes in the name of the algorithm requested and returns a Message Digest object that implements the specified algorithm. It can throw an exception NoSuchAlgorithmException, if the specified algorithm is not there in the provider.

So, if you want the MD5 hash, give MD5 as parameter in getInstance function of Class MessageDigest.

...