-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObtainSHA.java
49 lines (47 loc) · 1.64 KB
/
ObtainSHA.java
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
42
43
44
45
46
47
48
49
// author: Abhishek Agrawal(Almost everything is taken from the internet in this file)
// dateCreated: 01/03/2018
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
public class ObtainSHA {
public static int SHA1(String message) throws HashGenerationException {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashedBytes = digest.digest(message.getBytes("UTF-8"));
int key = Integer.parseInt(new BigInteger(convertToHex(hashedBytes), 16).toString(2).substring(1, Peer.m + 1), 2);
return key;
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
throw new HashGenerationException("Could not generate hash from String", ex);
}
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
}
class HashGenerationException extends Exception {
public HashGenerationException() {
super();
}
public HashGenerationException(String message, Throwable throwable) {
super(message, throwable);
}
public HashGenerationException(String message) {
super(message);
}
public HashGenerationException(Throwable throwable) {
super(throwable);
}
}