-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigestCalculator.java
213 lines (173 loc) · 7.97 KB
/
DigestCalculator.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Bernardo Bulgarelli - 2010468
// Camila Perez Aguiar - 1521516
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class DigestCalculator {
private static String digestType;
private static String pathToFolderWithFiles;
private static String pathToFileWithDigestList;
private static List<FileDigest> fileDigests;
private static List<FileDigest> newFilesDigests;
public static void readDigestList() throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(pathToFileWithDigestList));
doc.getDocumentElement().normalize();
NodeList entryList = doc.getElementsByTagName("FILE_ENTRY");
for(int i=0; i<entryList.getLength(); i++){
Node entryNode = entryList.item(i);
if(entryNode.getNodeType() == Node.ELEMENT_NODE){
Element fileElement = (Element) entryNode;
String fileName = fileElement.getElementsByTagName("FILE_NAME").item(0).getTextContent();
FileDigest file = new FileDigest(fileName);
NodeList digestList = fileElement.getElementsByTagName("DIGEST_ENTRY");
for(int j=0; j<digestList.getLength(); j++){
Node digestNode = digestList.item(j);
if(digestNode.getNodeType() == Node.ELEMENT_NODE){
Element digestElement = (Element) digestNode;
String digestType = digestElement.getElementsByTagName("DIGEST_TYPE").item(0).getTextContent();
String digestHex = digestElement.getElementsByTagName("DIGEST_HEX").item(0).getTextContent();
file.addDigestType(digestType, digestHex);
}
}
fileDigests.add(file);
}
}
}catch (ParserConfigurationException | SAXException | IOException e) {
throw e;
}
}
public static byte[] digestMessage(File file) throws IOException, NoSuchAlgorithmException{
MessageDigest messageDigest = MessageDigest.getInstance(digestType);
try(
InputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
){
byte[] buffer = new byte[1024];
int bytesRead;
while((bytesRead = bufferedInputStream.read(buffer)) != -1){
messageDigest.update(buffer, 0, bytesRead);
}
}
return messageDigest.digest();
}
public static String convertToHex(byte[] digest){
StringBuffer buf = new StringBuffer();
for(int i = 0; i < digest.length; i++) {
String hex = Integer.toHexString(0x0100 + (digest[i] & 0x00FF)).substring(1);
buf.append((hex.length() < 2 ? "0" : "") + hex);
}
return buf.toString();
}
public static void calculateAllFilesHash() throws FileNotFoundException, IOException, NoSuchAlgorithmException{
File folder = new File(pathToFolderWithFiles);
File[] files = folder.listFiles();
if(files == null){
System.out.println("ERRO! A pasta não existe ou não pode ser acessada");
throw new FileNotFoundException();
}
for(File file : files){
try{
if (file.isFile()) {
byte[] hash = digestMessage(file);
String hexHash = convertToHex(hash);
FileDigest fileDigest = new FileDigest(file.getName());
fileDigest.addDigestType(digestType, hexHash);
newFilesDigests.add(fileDigest);
}
}catch(FileNotFoundException e){
System.out.println("ERRO! O arquivo "+pathToFolderWithFiles+" não foi encontrado.");
}catch( IOException e){
System.out.println("ERRO processando o arquivo: "+file.getName());
throw e;
}catch( NoSuchAlgorithmException e){
System.out.println("ERRO! O algoritmo passado como argumento não é válido");
throw e;
}
}
}
public static void checkAndSaveDigests(){
for(FileDigest newFile : newFilesDigests){
String status = "";
for(FileDigest f : newFilesDigests){
if(!newFile.getFileName().equals(f.getFileName()) && newFile.getDigestHexByType(digestType).equals(f.getDigestHexByType(digestType))){
status = "COLISION";
}
}
if(!status.equals("COLISION")){
status = FileDigest.checkFileDigestStatus(fileDigests, newFile, digestType);
}
if(status.equals("NOT FOUND")){
FileDigest.addNewDigestToFileDigestList(fileDigests, newFile, digestType);
}
System.out.println(newFile.getFileName()+" "+digestType+" "+newFile.getDigestHexByType(digestType)+" "+"("+status+")");
}
}
public static void saveXMLFile() throws Exception{
try{
FileWriter writer = new FileWriter(pathToFileWithDigestList);
writer.write("<CATALOG>\n");
for(FileDigest f:fileDigests){
writer.write(" <FILE_ENTRY>\n");
writer.write(" <FILE_NAME>"+f.getFileName()+"</FILE_NAME>\n");
for(FileDigest.DigestType digestType : f.getDigestTypes()){
writer.write(" <DIGEST_ENTRY>\n");
writer.write(" <DIGEST_TYPE>"+digestType.getType()+"</DIGEST_TYPE>\n");
writer.write(" <DIGEST_HEX>"+digestType.getHex()+"</DIGEST_HEX>\n");
writer.write(" </DIGEST_ENTRY>\n");
}
writer.write(" </FILE_ENTRY>\n");
}
writer.write("</CATALOG>");
writer.close();
System.out.println("Novo CATALOGO salvo com sucesso!");
}catch(Exception e){
System.out.println("ERRO salvando o novo CATALOGO!");
throw e;
}
}
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("ERRO! A entrada do programa deve ser da seguinte forma: DigestCalculator <Tipo_Digest> <Caminho_da_Pasta_dos_Arquivos> <Caminho_ArqListaDigest>");
return;
}
digestType = args[0];
pathToFolderWithFiles = args[1];
pathToFileWithDigestList = args[2];
fileDigests = new ArrayList<>();
newFilesDigests = new ArrayList<>();
System.out.println("Tipo de Digest: " + digestType);
System.out.println("Caminho da Pasta dos Arquivos: " + pathToFolderWithFiles);
System.out.println("Caminho do Arquivo de Lista de Digest: " + pathToFileWithDigestList);
try{
System.out.println("\n\nCatalogo fornecido:");
readDigestList();
}catch(Exception e){
e.printStackTrace();
return;
}
FileDigest.printFileDigestList(fileDigests);
try{
System.out.println("\n\nHash dos arquivos fornecidos:");
calculateAllFilesHash();
}catch(Exception e){
return;
}
FileDigest.printFileDigestList(newFilesDigests);
checkAndSaveDigests();
try{
saveXMLFile();
}catch(Exception e){
return;
}
}
}