IMPLEMENTATION OF SHA-1 ALGORITHM using JAVA
AIM:
To write a java program to calculate the message
digest of a text using the SHA-1 algorithm
ALGORITHM:
Step 1: Input the
plain text
Step 2: Append padding
bits
Step 3: Append length
Step 4:Initialize buffer.
Step 5: Process
message in 512-bit (16word)blocks
Step 6: Encrypted in hexa decimal
format
Step 7: Display the encrypted
text.
PROGRAM:
importjava.security.*;
public class SHA1 {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info:
");
System.out.println(" Algorithm = "
+md.getAlgorithm());
System.out.println(" Provider = "
+md.getProvider());
System.out.println(" ToString = "
+md.toString());
String input
= "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\")
= " +bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\")
= " +bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""
+input+"\") = " +bytesToHex(output));
System.out.println(""); }
catch (Exception e) {
System.out.println("Exception: " +e);
}
}
public static String bytesToHex(byte[] b) {
char
hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F'};
StringBufferbuf = new StringBuffer();
for (int j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]); }
returnbuf.toString();
}
}
OUTPUT:
Comments
Post a Comment