Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (1.3k points)

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

2 Answers

0 votes
by (46k 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.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 14, 2019 in Java by Suresh (3.4k points)
0 votes
1 answer
asked Oct 29, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...