-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.java
47 lines (43 loc) · 1.38 KB
/
hash.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
package 作业4;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class hash {
public static void main(String[] args) {
try {
byte[] sha1= dfs("D:\\notebook");
String result="";
for(int i=0;i<sha1.length;i++){
result+=Integer.toString(sha1[i]&0xFF,16);
}
System.out.println(result);
}
catch (Exception e){
e.printStackTrace();
}
}
public static byte[] dfs(String path) throws Exception{
File dir= new File(path);
File[] fs=dir.listFiles();
byte[] buffer=new byte[1024];
MessageDigest complete = MessageDigest.getInstance("SHA-1");
for(int i=0;i<fs.length;i++){
if(fs[i].isFile()){
FileInputStream is= new FileInputStream(fs[i]);
int numRead=0;
do{
numRead=is.read(buffer);
if(numRead>0){
complete.update(buffer,0,numRead);
}
}while (numRead!=-1);
is.close();
}
if(fs[i].isDirectory()){
dfs(path+File.separator+fs[i].getName());
}
}
return complete.digest();
}
}