SHA1.java
1.74 KB
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
package com.qcloud.Utilities;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
public class SHA1 {
public static String stringToSHA(String str) {
try {
byte[] strTemp = str.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("SHA-1"); //SHA-256
mdTemp.update(strTemp);
return toHexString(mdTemp.digest());
} catch (Exception e) {
return null;
}
}
public static String fileNameToSHA(String fileName) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(fileName);
return streamToSHA(inputStream);
} catch (Exception e) {
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String streamToSHA(InputStream inputStream) {
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA-1"); //SHA-256
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = inputStream.read(buffer)) > 0) {
mdTemp.update(buffer, 0, numRead);
}
return toHexString(mdTemp.digest());
} catch (Exception e) {
return null;
}
}
private static String toHexString(byte[] md) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
int j = md.length;
char str[] = new char[j * 2];
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[2 * i] = hexDigits[byte0 >>> 4 & 0xf];
str[i * 2 + 1] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
public static void main(String[]args) {
System.out.println("start...");
System.out.println(SHA1.fileNameToSHA("d:\\test.rmvb"));
System.out.println("end...");
}
}