-
Notifications
You must be signed in to change notification settings - Fork 802
/
SHAUtils.java
49 lines (43 loc) · 1.42 KB
/
SHAUtils.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
package com.jingewenku.abrahamcaijin.commonutil.encryption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @Description:主要功能:SHA-1 加密 不可逆(Secure Hash Algorithm,安全散列算法)
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil.encryption
* @author: AbrahamCaiJin
* @date: 2017年05月16日 15:57
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class SHAUtils {
private SHAUtils() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* SHA-512 加密
* @param data
* @return
*/
public static String encryptSHA(byte[] data) {
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-512");
sha.update(data);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] resultBytes = sha.digest();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < resultBytes.length; i++) {
if (Integer.toHexString(0xFF & resultBytes[i]).length() == 1) {
builder.append("0").append(
Integer.toHexString(0xFF & resultBytes[i]));
} else {
builder.append(Integer.toHexString(0xFF & resultBytes[i]));
}
}
return builder.toString();
}
}