-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA512.java
62 lines (43 loc) · 1.21 KB
/
SHA512.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
50
51
52
53
54
55
56
57
58
59
60
61
62
package main;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA512 {
private MessageDigest md;
private String salt;
public String hash(String message, String salt) {
String ret = null;
try {
md = MessageDigest.getInstance("SHA-512");
this.salt = salt;
md.update(this.salt.getBytes());
byte[] b = md.digest(message.getBytes());
StringBuffer buff = new StringBuffer();
for(int i=0; i<b.length; ++i) {
buff.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
}
ret = buff.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return ret;
}
public String hash(String message) {
String ret = null;
try {
md = MessageDigest.getInstance("SHA-512");
md.update(this.salt.getBytes());
byte[] b = md.digest(message.getBytes());
StringBuffer buff = new StringBuffer();
for(int i=0; i<b.length; ++i) {
buff.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
}
ret = buff.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return ret;
}
public void setSalt(String salt) {
this.salt = salt;
}
}