Skip to content

Commit 06c65e3

Browse files
Add SHA-1 hash function implementation for generating secure hashes
1 parent 98bee26 commit 06c65e3

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.hashes;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Scanner;
7+
8+
/**
9+
* The SHA1 class provides a method to generate the SHA-1 hash for a given string.
10+
* SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces
11+
* a 160-bit (20-byte) hash value. It is commonly used for data integrity verification,
12+
* although it is no longer considered secure for cryptographic purposes.
13+
*
14+
* @see <a href="https://en.wikipedia.org/wiki/SHA-1">SHA-1 on Wikipedia</a>
15+
*/
16+
public class SHA1 {
17+
18+
/**
19+
* Generates the SHA-1 hash of the input string.
20+
*
21+
* @param input the string to be hashed
22+
* @return the SHA-1 hash as a hexadecimal string
23+
*/
24+
public static String getSha1(String input) {
25+
try {
26+
// Create a MessageDigest instance for SHA-1
27+
MessageDigest md = MessageDigest.getInstance("SHA-1");
28+
29+
// Compute the SHA-1 digest of the input string
30+
byte[] messageDigest = md.digest(input.getBytes());
31+
32+
// Convert the byte array into a BigInteger to get the hash in hexadecimal format
33+
BigInteger no = new BigInteger(1, messageDigest);
34+
35+
// Convert the BigInteger to a hexadecimal string
36+
String hashtext = no.toString(16);
37+
38+
// Pad with leading zeros to ensure the hash is 32 characters long
39+
while (hashtext.length() < 32) {
40+
hashtext = "0" + hashtext;
41+
}
42+
43+
// Return the generated SHA-1 hash
44+
return hashtext;
45+
} catch (NoSuchAlgorithmException e) {
46+
// Handle the case where SHA-1 algorithm is not available
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* The main method that takes user input, computes its SHA-1 hash, and prints the result.
53+
*
54+
* @param args command-line arguments
55+
* @throws NoSuchAlgorithmException if the SHA-1 algorithm is not available
56+
*/
57+
public static void main(String args[]) throws NoSuchAlgorithmException {
58+
// Create a Scanner object to read input from the user
59+
Scanner scanner = new Scanner(System.in);
60+
61+
// Prompt the user to enter a string to hash
62+
System.out.print("Enter the string to hash: ");
63+
64+
// Read the input string from the user
65+
String s = scanner.nextLine();
66+
67+
// Compute the SHA-1 hash of the input string and print it
68+
System.out.println("Your HashCode Generated by SHA-1 is: " + getSha1(s));
69+
70+
// Close the Scanner object
71+
scanner.close();
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.hashes;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Scanner;
7+
8+
/**
9+
* The SHA1 class provides a method to generate the SHA-1 hash for a given string.
10+
* SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces
11+
* a 160-bit (20-byte) hash value. It is commonly used for data integrity verification,
12+
* although it is no longer considered secure for cryptographic purposes.
13+
*
14+
* @see <a href="https://en.wikipedia.org/wiki/SHA-1">SHA-1 on Wikipedia</a>
15+
*/
16+
public class SHA1 {
17+
18+
/**
19+
* Generates the SHA-1 hash of the input string.
20+
*
21+
* @param input the string to be hashed
22+
* @return the SHA-1 hash as a hexadecimal string
23+
*/
24+
public static String getSha1(String input) {
25+
try {
26+
// Create a MessageDigest instance for SHA-1
27+
MessageDigest md = MessageDigest.getInstance("SHA-1");
28+
29+
// Compute the SHA-1 digest of the input string
30+
byte[] messageDigest = md.digest(input.getBytes());
31+
32+
// Convert the byte array into a BigInteger to get the hash in hexadecimal format
33+
BigInteger no = new BigInteger(1, messageDigest);
34+
35+
// Convert the BigInteger to a hexadecimal string
36+
String hashtext = no.toString(16);
37+
38+
// Pad with leading zeros to ensure the hash is 32 characters long
39+
while (hashtext.length() < 32) {
40+
hashtext = "0" + hashtext;
41+
}
42+
43+
// Return the generated SHA-1 hash
44+
return hashtext;
45+
} catch (NoSuchAlgorithmException e) {
46+
// Handle the case where SHA-1 algorithm is not available
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* The main method that takes user input, computes its SHA-1 hash, and prints the result.
53+
*
54+
* @param args command-line arguments
55+
* @throws NoSuchAlgorithmException if the SHA-1 algorithm is not available
56+
*/
57+
public static void main(String args[]) throws NoSuchAlgorithmException {
58+
// Create a Scanner object to read input from the user
59+
Scanner scanner = new Scanner(System.in);
60+
61+
// Prompt the user to enter a string to hash
62+
System.out.print("Enter the string to hash: ");
63+
64+
// Read the input string from the user
65+
String s = scanner.nextLine();
66+
67+
// Compute the SHA-1 hash of the input string and print it
68+
System.out.println("Your HashCode Generated by SHA-1 is: " + getSha1(s));
69+
70+
// Close the Scanner object
71+
scanner.close();
72+
}
73+
}

0 commit comments

Comments
 (0)