2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Best way to generate an MD5 hash

what is the best method to generate MD5 hash of a string in Java?
The MessageDigest class can provide you with an instance of the MD5 digest.

When working with strings and the crypto classes be sure to always specify the encoding you want the byte representation in. If you just use string.getBytes() it will use the platform default.

import java.security.*;
byte[] bytesOfMessage = yourString.getBytes("UTF-8");

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);

Then call .digest() to obtain the resulting hash.
Answer is