-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcnryptionImplementation.java
45 lines (35 loc) · 1.13 KB
/
EcnryptionImplementation.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
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EcnryptionImplementation {
public String encryptMessage(String message) {
try {
return toHexString(getSHA(message));
} catch (Exception e) {
return "";
}
}
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 64) {
hexString.insert(0, '0');
}
return hexString.toString();
}
// Driver code
public static void main(String args[]) {
}
}