-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Add MD5 hash function implementation for generating message digests #5335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
06c65e3
Add SHA-1 hash function implementation for generating secure hashes
karthikyandrapu ed97e40
Add MD5 hash function implementation for generating message digests
karthikyandrapu bffb6c8
Merge branch 'master' into MD5
karthikyandrapu eb48738
Update directory
karthikyandrapu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="https://en.wikipedia.org/wiki/MD5">MD5 on Wikipedia</a> | ||
*/ | ||
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="https://en.wikipedia.org/wiki/SHA-1">SHA-1 on Wikipedia</a> | ||
*/ | ||
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="https://en.wikipedia.org/wiki/MD5">MD5 on Wikipedia</a> | ||
*/ | ||
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"); | ||
Check failureCode scanning / CodeQL Use of a broken or risky cryptographic algorithm High test
Cryptographic algorithm
MD5 Error loading related location Loading |
||
|
||
// 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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="https://en.wikipedia.org/wiki/SHA-1">SHA-1 on Wikipedia</a> | ||
*/ | ||
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(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Use of a broken or risky cryptographic algorithm High