Skip to content

Commit 4151c6f

Browse files
authored
Update Java MD5.java
Added some comments to it so that it would be easier to understand for everyone.
1 parent 19a31bf commit 4151c6f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

Java MD5.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
1+
// Import statements for classes that are used in the code
12
import java.io.*;
23
import java.util.*;
34
import java.security.MessageDigest;
45
import java.util.Scanner;
56

6-
// Java 15
7-
7+
// Define the public class that contains the main method
88
public class Solution {
99

10+
// Main method that is executed when the program is run
1011
public static void main(String[] args) {
11-
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
12+
13+
// Create a new Scanner object to read input from standard input (keyboard)
1214
Scanner sc = new Scanner(System.in);
15+
16+
// Read a single string from the user and store it in the 'str' variable
1317
String str = sc.next();
18+
19+
// Close the scanner to free up system resources
1420
sc.close();
21+
22+
// Use a try-catch block to handle any exceptions that might be thrown
1523
try {
24+
25+
// Create a new MessageDigest object that uses the MD5 algorithm
1626
MessageDigest md = MessageDigest.getInstance("MD5");
27+
28+
// Update the MessageDigest object with the bytes of the input string
1729
md.update(str.getBytes());
18-
// return bytesToHex(md.digest
30+
31+
// Get the resulting digest as a byte array
1932
byte[] digest = md.digest();
33+
34+
// Loop through the byte array and print each byte as a hexadecimal string
2035
for (byte b : digest) {
2136
System.out.printf("%02x", b);
2237
}
38+
39+
// Catch any exceptions that might be thrown and re-throw them as a RuntimeException
2340
} catch (Exception ex) {
2441
throw new RuntimeException(ex);
2542
}
2643
}
2744
}
2845

29-
// Have a fantastic day.

0 commit comments

Comments
 (0)