Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
122 lines (100 loc) · 2.82 KB

File metadata and controls

122 lines (100 loc) · 2.82 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index">
<?dbhtml dir="security"?>
<title>java.security</title>
<section id="Security.getAlgorithms">
<title>列出 Java 支持的数字摘要算法</title>
<programlisting>
<![CDATA[
package cn.netkiller.security;
import java.security.Security;
import java.util.Set;
public class ListMessageDigest {
public static void main(String[] args) {
// List of available MessageDigest Algorithms
Set<String> messageDigest = Security.getAlgorithms("MessageDigest");
messageDigest.forEach(x -> System.out.println(x));
}
}
]]>
</programlisting>
<para>运行结果</para>
<screen>
<![CDATA[
SHA3-512
SHA-384
SHA
SHA3-384
SHA-224
SHA-512/256
SHA-256
MD2
SHA-512/224
SHA3-256
SHA-512
MD5
SHA3-224
]]>
</screen>
</section>
<section id="filesum">
<title>计算文件的 MD5,SHA 等 HASH 值</title>
<programlisting>
<![CDATA[
package cn.netkiller.security;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class sha1sum {
public sha1sum() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
String hex = checksum("/etc/hosts");
System.out.println(hex);
}
private static String checksum(String filepath) throws IOException, NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA"); // SHA, MD2, MD5, SHA-256, SHA-384...
// file hashing with DigestInputStream
try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) {
while (dis.read() != -1)
; // empty loop to clear the data
md = dis.getMessageDigest();
}
// bytes to hex
StringBuilder result = new StringBuilder();
for (byte b : md.digest()) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
]]>
</programlisting>
</section>
<section>
<title>Md5sum</title>
<programlisting>
<![CDATA[
public String md5sum(String filename) throws NoSuchAlgorithmException, IOException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(Files.readAllBytes(Paths.get(filename)));
byte[] digest = messageDigest.digest();
String checksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
return checksum;
}
public boolean checksum() throws NoSuchAlgorithmException {
String hash = "35454B055CC325EA1AF2126E27707052";
String password = "ILoveJava";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase();
return myHash.equals(hash);
}
]]>
</programlisting>
</section>
</chapter>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.