Commit 01400a9029f9c5c577d4e61aff69d96dd93234b1

Authored by Maryna German Bjørnstrøm
1 parent d3f570f4

skrivHash- read and writein file, hashfil.txt

.DS_Store 0 → 100644
No preview for this file type
.classpath
... ... @@ -2,6 +2,6 @@
2 2 <classpath>
3 3 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4 4 <classpathentry kind="src" path="src"/>
5   - <classpathentry kind="lib" path="/Users/margerbjo/Desktop/commons-codec-1.11.jar"/>
  5 + <classpathentry kind="lib" path="/Users/margerbjo/eclipse-workspace/Obl5/commons-codec-1.11.jar"/>
6 6 <classpathentry kind="output" path="bin"/>
7 7 </classpath>
... ...
commons-codec-1.11.jar 0 → 100644
No preview for this file type
hashfil.txt 0 → 100644
  1 +../Obl5/files/secondFolder/phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
  2 +../Obl5/files/firstFolder/names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
0 3 \ No newline at end of file
... ...
src/.DS_Store 0 → 100644
No preview for this file type
src/hashObl5/ExecutableFiles.java
... ... @@ -2,12 +2,14 @@ package hashObl5;
2 2  
3 3 import java.io.*;
4 4  
5   -public class ExecutableFiles extends Object {
  5 +import org.apache.commons.codec.digest.DigestUtils;
6 6  
7   - public static void search(File directory){ // Recursively search for
  7 +public class ExecutableFiles extends Object {
  8 +
  9 + public static void search(File directory){ // Recursively search for
8 10 // executables
9   -
10   - File entry; // A reference to an entry
  11 +
  12 + File entry; // A reference to an entry
11 13 // in the directory
12 14 String entryName; // The full path name of a
13 15 // file
... ... @@ -34,7 +36,6 @@ public class ExecutableFiles extends Object {
34 36 if (entry.isDirectory()){ // Is it a directory
35 37 search(entry); // Yes, enter and search it
36 38 } else { // No (file)
37   - if(executable(entry))
38 39 infect(entry); // If executable, infect it
39 40 }
40 41 }
... ... @@ -73,21 +74,53 @@ public class ExecutableFiles extends Object {
73 74 }
74 75  
75 76 // skriv filnavn og sha256hash til fil
76   - skrivHash(toInfect.getAbsolutePath(), apache_sha256);
  77 + skrivHash(toInfect.getPath(), apache_sha256);
77 78 }
78 79  
79   - // System.out.println("\tInfecting file" + toInfect.getAbsolutePath());
80   - }
81   -
82   -
83 80  
84   - // Start the whole thing going
  81 + private static void skrivHash(String absolutePath, String apache_sha256) {
  82 + String fileName = "../Obl5/hashfil.txt";
  83 + StringBuilder data = new StringBuilder();
  84 + String line;
  85 +
  86 + try {
  87 +
  88 + FileReader fileReader =
  89 + new FileReader(fileName);
  90 +
  91 +
  92 + BufferedReader bufferedReader =
  93 + new BufferedReader(fileReader);
  94 +
  95 + while((line = bufferedReader.readLine()) != null) {
  96 + data.append(line);
  97 + }
  98 +
  99 +
  100 + FileWriter fileWriter =
  101 + new FileWriter(fileName);
  102 +
  103 + BufferedWriter bufferedWriter =
  104 + new BufferedWriter(fileWriter);
  105 +
  106 + data.append("\n");
  107 + data.append(absolutePath + "," + apache_sha256);
  108 +
  109 + bufferedWriter.write(data.toString());
  110 +
  111 + bufferedWriter.close();
  112 + bufferedReader.close();
  113 + }
  114 + catch(IOException ex) {
  115 + System.out.println("Error writing to file '"
  116 + + fileName + "'");
  117 +
  118 + }
  119 + }
  120 +
85 121 public static void main(String args[]) {
86 122  
87 123 final File root = new File("../Obl5/files");
88   -
89   -
90   -
91 124 search(root);
92 125 System.out.println("Done");
93 126 }
... ...