Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. class OpenAndWriteFile implements PublicInterfaces.WriteToDisk {
  2.  
  3. private ArrayList<String> tempImportList = new ArrayList<String>();
  4. private ArrayList<String> Directories = new ArrayList<String>();
  5. private ArrayList<String> Filenames = new ArrayList<String>();
  6. private ArrayList<Integer> HashFunctionUsed = new ArrayList<Integer>();
  7. private ArrayList<Integer> inspectionSelection = new ArrayList<Integer>();
  8. private ArrayList<Long> Hashs = new ArrayList<Long>();
  9. private Set<String> tamperedList = new HashSet<String>();
  10. private Set<String> tamperedName = new HashSet<String>();
  11. private String nextFileLine;
  12. private Set<String> lineToRemove = new HashSet<String>();
  13. private Set<String> lineToKeep = new HashSet<String>();
  14. private String Hash;
  15. private File file = new File("");
  16. private String fullFormattedStored;
  17. private PublicInterfaces.DialogInterface interForDialogs = new Dialogs();
  18.  
  19. public OpenAndWriteFile(String fileToScan, String directory, String fileName, int hashFunction, long Hash, int inspectionSelect) throws IOException {
  20. String fullFormatted = directory + " : " + fileName + " : " + hashFunction + " : " + inspectionSelect + " : " + Hash;
  21. String formatted = directory + " : " + fileName + " : " + hashFunction + " : " + inspectionSelect;
  22. this.Hash = String.format("%016X", Hash);
  23.  
  24. try {
  25. /**
  26. * Iterate over all lines of the file, add them to a list.
  27. */
  28. BufferedReader in = new BufferedReader(new FileReader(fileToScan));
  29. while ((nextFileLine = in.readLine()) != null){
  30. tempImportList.add(nextFileLine);
  31. }
  32. in.close();
  33.  
  34. if(tempImportList.isEmpty()) {
  35. file = new File("temp.txt");
  36. File finalName = new File("Final.txt");
  37. BufferedWriter writeFinal = new BufferedWriter(new FileWriter(file, true));
  38.  
  39. if(finalName.exists()) {
  40. boolean delete = finalName.delete();
  41.  
  42. if(delete) {
  43. writeFinal.write(fullFormatted);
  44. writeFinal.newLine();
  45. writeFinal.close();
  46. boolean success = file.renameTo(finalName);
  47.  
  48. if(success) {
  49. lineToKeep.add(fullFormatted);
  50. WriteFile("Final.txt");
  51. } else {
  52. System.out.println("Unsucessfuly renamed " + file.getName() + " to " +finalName.getName());
  53. }
  54. } else {
  55. System.out.println("Unsucessfuly deleted " +finalName.getName());
  56. }
  57.  
  58. }
  59. } else {
  60. /**
  61. * Split that list into a conventional array on every colon.
  62. */
  63. for(int i = 0; i < tempImportList.size(); i++ ) {
  64. String[] importedData = tempImportList.get(i).split(" : ");
  65. Directories.add(importedData[0].trim());
  66. Filenames.add(importedData[1].trim());
  67. HashFunctionUsed.add(Integer.parseInt(importedData[2].trim()));
  68. inspectionSelection.add(Integer.parseInt(importedData[3].trim()));
  69. Hashs.add(Long.parseLong(importedData[4].trim()));
  70. }
  71.  
  72. /**
  73. * set1 includes the entire strings from the imported data.
  74. */
  75. ArrayList<String> set1 = new ArrayList<String>();
  76. for(int i = 0; i < Directories.size(); i++) {
  77. set1.add(Directories.get(i) + " : " + Filenames.get(i) + " : " + HashFunctionUsed.get(i) + " : " + inspectionSelection.get(i) + " : " + Hashs.get(i));
  78. Collections.sort(set1);
  79. }
  80.  
  81. /**
  82. * set2 includes the entire strings from the imported data except the Hash.
  83. */
  84. ArrayList<String> set2 = new ArrayList<String>();
  85. for(int i = 0; i < Directories.size(); i++) {
  86. set2.add(Directories.get(i) + " : " + Filenames.get(i) + " : " + HashFunctionUsed.get(i) + " : " + inspectionSelection.get(i));
  87. Collections.sort(set2);
  88. }
  89.  
  90. /**
  91. * set3 is a list of all the hashes that have been parsed from the file.
  92. */
  93. ArrayList<Long> set3 = new ArrayList<Long>();
  94. for(int i = 0; i < Hashs.size(); i++) {
  95. set3.add(Hashs.get(i));
  96. Collections.sort(set3);
  97. }
  98.  
  99. for(int i = 0; i < Directories.size(); i++) {
  100. fullFormattedStored = Directories.get(i) + " : " + Filenames.get(i) + " : " + HashFunctionUsed.get(i) + " : " + inspectionSelection.get(i) + " : " + Hashs.get(i);
  101.  
  102. if(set1.contains(fullFormatted)) {
  103. lineToKeep.add(fullFormattedStored);
  104. lineToRemove.add("(duplicate) : " + fullFormatted);
  105. continue;
  106. } else if(set2.contains(formatted)) {
  107. for(Long s : set3) {
  108. if(set3.contains(Hash)) {
  109. lineToKeep.add(fullFormatted);
  110. lineToRemove.add("old stored (duplicate): " + fullFormattedStored);
  111. continue;
  112. } else if(set3.contains(Hash) == false && s != Hash) {
  113. for(int j = 0; j < Directories.size(); j++) {
  114. if(String.valueOf(Directories.get(i) + " : " + Filenames.get(i) + " : " + HashFunctionUsed.get(i) + " : " + inspectionSelection.get(i)).equals(String.valueOf(directory + " : " + fileName + " : " + hashFunction + " : " + inspectionSelect))) {
  115. lineToRemove.add("(tampered) : " + fullFormattedStored);
  116. lineToKeep.add(fullFormatted);
  117. tamperedName.add(fileName);
  118. tamperedList.add("Old hash: " +String.format("%016X", Hashs.get(i)) + "\nNew hash: " + this.Hash);
  119.  
  120. if(lineToKeep.contains(fullFormattedStored)) {
  121. lineToKeep.remove(fullFormattedStored);
  122. }
  123. continue;
  124.  
  125. } else if(String.valueOf(Filenames.get(i) + " : " + HashFunctionUsed.get(i) + " : " + inspectionSelection.get(i)).equals(String.valueOf(fileName + " : " + hashFunction + " : " + inspectionSelect))) {
  126. lineToRemove.add("(tampered) : " + fullFormattedStored);
  127. lineToKeep.add(fullFormatted);
  128. tamperedName.add(fileName);
  129. tamperedList.add("Old hash: " +String.format("%016X", Hashs.get(i)) + "\nNew hash: " + this.Hash);
  130.  
  131. if(lineToKeep.contains(fullFormattedStored)) {
  132. lineToKeep.remove(fullFormattedStored);
  133. }
  134.  
  135. continue;
  136.  
  137. } else {
  138. lineToKeep.add(fullFormattedStored);
  139. continue;
  140. }
  141. }
  142. }
  143. }
  144. } else {
  145. lineToKeep.add(fullFormatted);
  146. lineToKeep.add(fullFormattedStored);
  147. continue;
  148. }
  149. }
  150. }
  151. /**
  152. * If the file is not found then we can assume the file doesn't exist.
  153. * If that is the case, write to the file the current passed through file information.
  154. */
  155. } catch (FileNotFoundException e) {
  156. file = new File("temp.txt");
  157. File newFileName = new File("Final.txt");
  158. BufferedWriter writeFinal = new BufferedWriter(new FileWriter(file, true));
  159. writeFinal.append(fullFormatted);
  160. writeFinal.newLine();
  161. writeFinal.close();
  162. boolean success = file.renameTo(newFileName);
  163.  
  164. if(success) {
  165. System.out.println("Sucessfuly renamed " + file.getName() + " to " +newFileName.getName());
  166. } else {
  167. System.out.println("Unsucessfuly renamed " + file.getName() + " to " +newFileName.getName());
  168. }
  169. } finally {
  170. WriteFile("Final.txt");
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement