forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_encryption.java
49 lines (41 loc) · 1.49 KB
/
password_encryption.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
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class password_encryption
{
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
/* MessageDigest instance for hashing using SHA512*/
MessageDigest md = MessageDigest.getInstance("SHA-512");
/* 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 of hash into digest */
BigInteger number = new BigInteger(1, hash);
/* Convert the digest into hex value */
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 32)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
/* Driver code */
public static void main(String args[])
{
try
{
String string1 = "examplePassword123";
System.out.println("\n" + string1 + " : " + toHexString(getSHA(string1)));
String string2 = "hashtrial";
System.out.println("\n" + string2 + " : " + toHexString(getSHA(string2)));
}
catch (NoSuchAlgorithmException e)
{
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
}