diff --git a/DIRECTORY.md b/DIRECTORY.md index 656597c3b20a..55600000a88b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -103,7 +103,7 @@ * [BoruvkaAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java) * [ConnectedComponent](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/ConnectedComponent.java) * [Cycles](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Cycles.java) - * [DIJSKSTRAS ALGORITHM](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java) + * [DijkstraAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java) * [FloydWarshall](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java) * [Graphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java) * [HamiltonianCycle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java) @@ -263,6 +263,9 @@ * [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java) * [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java) * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java) + * hashes + * [MD5](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/hashes/MD5.java) + * [SHA1](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/hashes/SHA1.java) * io * [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java) * maths @@ -614,6 +617,7 @@ * [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.java) * [BinaryToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToOctalTest.java) * [DecimalToHexaDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToHexaDecimalTest.java) + * [DecimalToOctalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/DecimalToOctalTest.java) * [HexaDecimalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java) * [HexaDecimalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java) * [HexToOctTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.java) @@ -642,6 +646,7 @@ * [DisjointSetUnionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnionTest.java) * graphs * [BoruvkaAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java) + * [DijkstraAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java) * [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java) * [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java) * [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java) @@ -714,6 +719,9 @@ * [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java) * [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java) * [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java) + * hashes + * [MD5](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/hashes/MD5.java) + * [SHA1](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/hashes/SHA1.java) * io * [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java) * maths @@ -886,6 +894,7 @@ * [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java) * [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java) * [PatienceSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PatienceSortTest.java) + * [PigeonholeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java) * [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java) * [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java) * [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java) diff --git a/src/main/java/com/thealgorithms/hashes/MD5.java b/src/main/java/com/thealgorithms/hashes/MD5.java new file mode 100644 index 000000000000..ab15833d2efd --- /dev/null +++ b/src/main/java/com/thealgorithms/hashes/MD5.java @@ -0,0 +1,73 @@ +package com.thealgorithms.hashes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Scanner; + +/** + * The MD5 class provides a method to generate the MD5 hash for a given string. + * MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces + * a 128-bit (16-byte) hash value. It is commonly used for checksums and data integrity, + * although it is no longer considered secure for cryptographic purposes. + * + * @see MD5 on Wikipedia + */ +public class MD5 { + + /** + * Generates the MD5 hash of the input string. + * + * @param input the string to be hashed + * @return the MD5 hash as a hexadecimal string + */ + public static String getMd5(String input) { + try { + // Create a MessageDigest instance for MD5 + MessageDigest md = MessageDigest.getInstance("MD5"); + + // Compute the MD5 digest of the input string + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert the byte array into a BigInteger to get the hash in hexadecimal format + BigInteger no = new BigInteger(1, messageDigest); + + // Convert the BigInteger to a hexadecimal string + String hashtext = no.toString(16); + + // Pad with leading zeros to ensure the hash is 32 characters long + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // Return the generated MD5 hash + return hashtext; + } catch (NoSuchAlgorithmException e) { + // Handle the case where MD5 algorithm is not available + throw new RuntimeException(e); + } + } + + /** + * The main method that takes user input, computes its MD5 hash, and prints the result. + * + * @param args command-line arguments + * @throws NoSuchAlgorithmException if the MD5 algorithm is not available + */ + public static void main(String args[]) throws NoSuchAlgorithmException { + // Create a Scanner object to read input from the user + Scanner scanner = new Scanner(System.in); + + // Prompt the user to enter a string to hash + System.out.print("Enter the string to hash: "); + + // Read the input string from the user + String s = scanner.nextLine(); + + // Compute the MD5 hash of the input string and print it + System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s)); + + // Close the Scanner object + scanner.close(); + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/hashes/SHA1.java b/src/main/java/com/thealgorithms/hashes/SHA1.java new file mode 100644 index 000000000000..cfc9b4dac460 --- /dev/null +++ b/src/main/java/com/thealgorithms/hashes/SHA1.java @@ -0,0 +1,73 @@ +package com.thealgorithms.hashes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Scanner; + +/** + * The SHA1 class provides a method to generate the SHA-1 hash for a given string. + * SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces + * a 160-bit (20-byte) hash value. It is commonly used for data integrity verification, + * although it is no longer considered secure for cryptographic purposes. + * + * @see SHA-1 on Wikipedia + */ +public class SHA1 { + + /** + * Generates the SHA-1 hash of the input string. + * + * @param input the string to be hashed + * @return the SHA-1 hash as a hexadecimal string + */ + public static String getSha1(String input) { + try { + // Create a MessageDigest instance for SHA-1 + MessageDigest md = MessageDigest.getInstance("SHA-1"); + + // Compute the SHA-1 digest of the input string + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert the byte array into a BigInteger to get the hash in hexadecimal format + BigInteger no = new BigInteger(1, messageDigest); + + // Convert the BigInteger to a hexadecimal string + String hashtext = no.toString(16); + + // Pad with leading zeros to ensure the hash is 32 characters long + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // Return the generated SHA-1 hash + return hashtext; + } catch (NoSuchAlgorithmException e) { + // Handle the case where SHA-1 algorithm is not available + throw new RuntimeException(e); + } + } + + /** + * The main method that takes user input, computes its SHA-1 hash, and prints the result. + * + * @param args command-line arguments + * @throws NoSuchAlgorithmException if the SHA-1 algorithm is not available + */ + public static void main(String args[]) throws NoSuchAlgorithmException { + // Create a Scanner object to read input from the user + Scanner scanner = new Scanner(System.in); + + // Prompt the user to enter a string to hash + System.out.print("Enter the string to hash: "); + + // Read the input string from the user + String s = scanner.nextLine(); + + // Compute the SHA-1 hash of the input string and print it + System.out.println("Your HashCode Generated by SHA-1 is: " + getSha1(s)); + + // Close the Scanner object + scanner.close(); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/hashes/MD5.java b/src/test/java/com/thealgorithms/hashes/MD5.java new file mode 100644 index 000000000000..ab15833d2efd --- /dev/null +++ b/src/test/java/com/thealgorithms/hashes/MD5.java @@ -0,0 +1,73 @@ +package com.thealgorithms.hashes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Scanner; + +/** + * The MD5 class provides a method to generate the MD5 hash for a given string. + * MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces + * a 128-bit (16-byte) hash value. It is commonly used for checksums and data integrity, + * although it is no longer considered secure for cryptographic purposes. + * + * @see MD5 on Wikipedia + */ +public class MD5 { + + /** + * Generates the MD5 hash of the input string. + * + * @param input the string to be hashed + * @return the MD5 hash as a hexadecimal string + */ + public static String getMd5(String input) { + try { + // Create a MessageDigest instance for MD5 + MessageDigest md = MessageDigest.getInstance("MD5"); + + // Compute the MD5 digest of the input string + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert the byte array into a BigInteger to get the hash in hexadecimal format + BigInteger no = new BigInteger(1, messageDigest); + + // Convert the BigInteger to a hexadecimal string + String hashtext = no.toString(16); + + // Pad with leading zeros to ensure the hash is 32 characters long + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // Return the generated MD5 hash + return hashtext; + } catch (NoSuchAlgorithmException e) { + // Handle the case where MD5 algorithm is not available + throw new RuntimeException(e); + } + } + + /** + * The main method that takes user input, computes its MD5 hash, and prints the result. + * + * @param args command-line arguments + * @throws NoSuchAlgorithmException if the MD5 algorithm is not available + */ + public static void main(String args[]) throws NoSuchAlgorithmException { + // Create a Scanner object to read input from the user + Scanner scanner = new Scanner(System.in); + + // Prompt the user to enter a string to hash + System.out.print("Enter the string to hash: "); + + // Read the input string from the user + String s = scanner.nextLine(); + + // Compute the MD5 hash of the input string and print it + System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s)); + + // Close the Scanner object + scanner.close(); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/hashes/SHA1.java b/src/test/java/com/thealgorithms/hashes/SHA1.java new file mode 100644 index 000000000000..cfc9b4dac460 --- /dev/null +++ b/src/test/java/com/thealgorithms/hashes/SHA1.java @@ -0,0 +1,73 @@ +package com.thealgorithms.hashes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Scanner; + +/** + * The SHA1 class provides a method to generate the SHA-1 hash for a given string. + * SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces + * a 160-bit (20-byte) hash value. It is commonly used for data integrity verification, + * although it is no longer considered secure for cryptographic purposes. + * + * @see SHA-1 on Wikipedia + */ +public class SHA1 { + + /** + * Generates the SHA-1 hash of the input string. + * + * @param input the string to be hashed + * @return the SHA-1 hash as a hexadecimal string + */ + public static String getSha1(String input) { + try { + // Create a MessageDigest instance for SHA-1 + MessageDigest md = MessageDigest.getInstance("SHA-1"); + + // Compute the SHA-1 digest of the input string + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert the byte array into a BigInteger to get the hash in hexadecimal format + BigInteger no = new BigInteger(1, messageDigest); + + // Convert the BigInteger to a hexadecimal string + String hashtext = no.toString(16); + + // Pad with leading zeros to ensure the hash is 32 characters long + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // Return the generated SHA-1 hash + return hashtext; + } catch (NoSuchAlgorithmException e) { + // Handle the case where SHA-1 algorithm is not available + throw new RuntimeException(e); + } + } + + /** + * The main method that takes user input, computes its SHA-1 hash, and prints the result. + * + * @param args command-line arguments + * @throws NoSuchAlgorithmException if the SHA-1 algorithm is not available + */ + public static void main(String args[]) throws NoSuchAlgorithmException { + // Create a Scanner object to read input from the user + Scanner scanner = new Scanner(System.in); + + // Prompt the user to enter a string to hash + System.out.print("Enter the string to hash: "); + + // Read the input string from the user + String s = scanner.nextLine(); + + // Compute the SHA-1 hash of the input string and print it + System.out.println("Your HashCode Generated by SHA-1 is: " + getSha1(s)); + + // Close the Scanner object + scanner.close(); + } +} \ No newline at end of file