Is there any method to generate MD5 hash of a string in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
There is a
DigestUtils
class in Spring also:http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/util/DigestUtils.html
This class contains the method
md5DigestAsHex()
that does the job.Unlike PHP where you can do an MD5 hashing of your text by just calling md5 function ie
md5($text)
, in Java it was made little bit complicated. I usually implemented it by calling a function which returns the md5 hash text. Here is how I implemented it, First create a function namedmd5hashing
inside your main class as given below.Now call the function whenever you needed as given below.
Here you can see that hashtext is appended with a zero to make it match with md5 hashing in PHP.
Found this:
on the site below, I take no credit for it, but its a solution that works! For me lots of other code didnt work properly, I ended up missing 0s in the hash. This one seems to be the same as PHP has. source: http://m2tec.be/blog/2010/02/03/java-md5-hex-0093
You might also want to look at the DigestUtils class of the apache commons codec project, which provides very convenient methods to create MD5 or SHA digests.