How to compute MD5 hash of a string in Java?
Hi everyone! I just started learning Java and came across a task: I need to compute the MD5 hash of a string to check file integrity. I've searched through many forums, but everywhere the examples are either outdated or too complicated for a beginner.
I tried to write the code myself, but it doesn't work. Here's what I got:
```java
public class MD5Example {
public static void main(String[] args) {
String input = "Hello, world!";
String hash = input.hashCode(); // I know this is wrong
System.out.println("MD5: " + hash);
}
}
```
I understand that `hashCode()` is not MD5, just Java's internal hash. But how to do it correctly? I've heard about `MessageDigest`, but I don't understand how to use it. Do I need to include any libraries? Or can I do it with standard tools?
No answers yet.
Log in to leave an answer