Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

EX NO: 2(E) SHA-1

AIM:
To implement the SHA-1 algorithm in java

ALGORITHM

• Start the program.


• Declare the class and required variables.
• Implement the SHA-1 algorithm
• Create the object for the class in the main program.
• Access the member functions using the objects.
• Stop the program.

PROGRAM CODE

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*manual prepared by www.gr-solution.blogspot.com*/
public class HashTextTest {

/**
* @param args
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("Encryption text is=");
System.out.println(sha1("test string to sha1"));
}
/*manual prepared by www.gr-solution.blogspot.com*/
static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}

return sb.toString();
}
}
OUTPUT

You might also like