How can I generate an MD5 hash?
Posted By: Akshay
Is there any method to generate MD5 hash of a string in Java?
Solution
You need java.security.MessageDigest
.
Call MessageDigest.getInstance("MD5")
to get a MD5 instance of MessageDigest
you can use.
The compute the hash by doing one of:
- Feed the entire input as a
byte[]
and calculate the hash in one operation withmd.digest(bytes)
. - Feed the
MessageDigest
onebyte[]
chunk at a time by callingmd.update(bytes)
. When you’re done adding input bytes, calculate the hash with
md.digest()
.
The byte[]
returned by md.digest()
is the MD5 hash.
Answered By: Bombe
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.