Professional Documents
Culture Documents
EX NO: 2 (E) SHA-1 Aim
EX NO: 2 (E) SHA-1 Aim
AIM:
To implement the SHA-1 algorithm in java
ALGORITHM
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