-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMD.java
47 lines (37 loc) · 1.39 KB
/
MD.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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD {
public static void main(String[] agrs) throws NoSuchAlgorithmException, IOException
{
PrintWriter out = new PrintWriter("test.txt");
out.println("This is a test");
out.close();
MessageDigest md = MessageDigest.getInstance("SHA-1");
FileInputStream fis = new FileInputStream("test.txt");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
fis.close();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Hex format : " + sb.toString());
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i=0;i<mdbytes.length;i++) {
hexString.append(Integer.toHexString(0xFF & mdbytes[i]));
}
System.out.println("Hex format : " + hexString.toString());
}
}