sha1
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class SHA1HashExample {
public static String calculateSHA1(String input) {
try {
// Create a MessageDigest instance for SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");
// Calculate the digest
byte[] messageDigest = md.digest(input.getBytes());
// Convert byte array into hex representation
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
hexString.append(String.format("%02x", b)); // lower-case hex
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-1 algorithm not found");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter text to hash using SHA-1: ");
String input = sc.nextLine();
String sha1Hash = calculateSHA1(input);
System.out.println("SHA-1 Hash: " + sha1Hash);
}
}