Repository navigation
Expand file tree
/
Copy pathsha1.java
More file actions
42 lines (41 loc) · 1.38 KB
/
Copy pathsha1.java
File metadata and controls
42 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cns;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class sha1 {
public static void main(String args[]) throws NoSuchAlgorithmException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter input String(Plain text Message): ");
String str = sc.next();
String hashcode;
MessageDigest md;
try {
// getInstance() method is called with algorithm SHA-512
md = MessageDigest.getInstance("SHA1");
// digest() method is called
// to calculate message digest of the input string str
// returned as array of byte
byte[] messageDigest = md.digest(str.getBytes());
// Convert byte array into signum representation
BigInteger no = new BigInteger(1, messageDigest);
// Convert message digest into hex value
hashcode = no.toString(16);
// Add preceding 0s to make it 32 bit
while (hashcode.length() < 32) {
hashcode = "0" + hashcode;
}
}
// For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
System.out.println("HashCode Generated by SHA-512 for: ");
System.out.println("\n" + str + " is : " + hashcode);
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());
}
}