Skip to content

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/main/java/com/thealgorithms/hashes/MD5.java
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 failure

Code scanning / CodeQL

Use of a broken or risky cryptographic algorithm High

Cryptographic algorithm
MD5
is weak and should not be used.

// 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();
}
}
73 changes: 73 additions & 0 deletions src/main/java/com/thealgorithms/hashes/SHA1.java
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();
}
}
73 changes: 73 additions & 0 deletions src/test/java/com/thealgorithms/hashes/MD5.java
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 failure

Code scanning / CodeQL

Use of a broken or risky cryptographic algorithm High test

Cryptographic algorithm
MD5
is weak and should not be used.

// 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();
}
}
73 changes: 73 additions & 0 deletions src/test/java/com/thealgorithms/hashes/SHA1.java
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();
}
}
Loading