MCPcopy Create free account
hub / github.com/SeanDragon/protools / ToolSHA2

Class ToolSHA2

security/src/main/java/pro/tools/security/md/ToolSHA2.java:15–167  ·  view source on GitHub ↗

SHA-2 的变种 SHA- 224、 SHA- 256、 SHA-384 、 SHA-512 @author SeanDragon

Source from the content-addressed store, hash-verified

13 * @author SeanDragon
14 */
15public final class ToolSHA2 {
16 private ToolSHA2() {
17 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
18 }
19
20 /**
21 * SHA-224加密
22 *
23 * @param data
24 * 待加密数据
25 *
26 * @return byte[] 消息摘要
27 *
28 * @throws Exception
29 */
30 public static byte[] encodeSHA224(byte[] data) throws NoSuchAlgorithmException {
31 // 加入BouncyCastleProvider支持
32 Security.addProvider(new BouncyCastleProvider());
33
34 // 初始化MessageDigest
35 MessageDigest md = MessageDigest.getInstance("SHA-224");
36
37 // 执行消息摘要
38 return md.digest(data);
39 }
40
41 /**
42 * SHA-224加密
43 *
44 * @param data
45 * 待加密数据
46 *
47 * @return byte[] 消息摘要
48 *
49 * @throws Exception
50 */
51 public static String encodeSHA224Hex(byte[] data) throws NoSuchAlgorithmException {
52 // 执行消息摘要
53 byte[] b = encodeSHA224(data);
54
55 // 做十六进制编码处理
56 return new String(Hex.encode(b));
57 }
58
59 /**
60 * SHA-256加密
61 *
62 * @param data
63 * 待加密数据
64 *
65 * @return byte[] 消息摘要
66 *
67 * @throws Exception
68 */
69 public static byte[] encodeSHA256(byte[] data) throws NoSuchAlgorithmException {
70 // 初始化MessageDigest
71 MessageDigest md = MessageDigest.getInstance("SHA-256");
72

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected