100% found this document useful (1 vote)
626 views2 pages

Title: Calculate The Message Digest of A Text Using The SHA-1 Algorithm in JAVA. Code

This Java code uses the SHA-1 algorithm to generate a message digest or hash of input text. It imports the necessary classes, defines a method to encrypt a string by applying SHA-1 and returning the hashed output, and includes a main method that tests it on two sample inputs and prints the results.

Uploaded by

Amol Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
626 views2 pages

Title: Calculate The Message Digest of A Text Using The SHA-1 Algorithm in JAVA. Code

This Java code uses the SHA-1 algorithm to generate a message digest or hash of input text. It imports the necessary classes, defines a method to encrypt a string by applying SHA-1 and returning the hashed output, and includes a main method that tests it on two sample inputs and prints the results.

Uploaded by

Amol Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Title : Calculate the message digest of a text using the SHA-1 algorithm in JAVA.

Code :
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class P7_SHA_1 {


public static String encryptThisString(String input)
{
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String args[]) throws
NoSuchAlgorithmException
{
System.out.println("\n\nHashCode By SHA-1 Algorithm ");

String s1 = "Vilas Sonje";


System.out.println("\n" + s1 + " : " + encryptThisString(s1));

String s2 = "CNS LAB 6";


System.out.println("\n" + s2 + " : " + encryptThisString(s2)+"\n\n");
}
}

Output :

You might also like