Commit d3f570f4f4e4990bdb3dc2e1ffa41b2a9c62feee

Authored by Maryna German Bjørnstrøm
1 parent 2ea5a1eb

class ExecutableFiles, names.txt, phones.txt

.classpath
... ... @@ -2,5 +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 6 <classpathentry kind="output" path="bin"/>
6 7 </classpath>
... ...
files/firstFolder/names.txt 0 → 100644
  1 +Heidi
  2 +John
  3 +Marina
  4 +Kevin
  5 +Liam
0 6 \ No newline at end of file
... ...
files/secondFolder/phones.txt 0 → 100644
  1 +45636353
  2 +23762537
  3 +27378263
  4 +23762362
  5 +72636255
0 6 \ No newline at end of file
... ...
src/hashObl5/ExecutableFiles.java 0 → 100644
  1 +package hashObl5;
  2 +
  3 +import java.io.*;
  4 +
  5 +public class ExecutableFiles extends Object {
  6 +
  7 + public static void search(File directory){ // Recursively search for
  8 + // executables
  9 +
  10 + File entry; // A reference to an entry
  11 + // in the directory
  12 + String entryName; // The full path name of a
  13 + // file
  14 +
  15 + System.out.println("Starting search of directory "
  16 + + directory.getAbsolutePath());
  17 +
  18 + if(directory == null) return; // Could not be opened;
  19 + // forget it
  20 +
  21 +
  22 + String contents[] = directory.list(); // Get an array of all the
  23 + // files in the directory
  24 + if(contents == null) return; // Could not access
  25 + // contents, skip it
  26 +
  27 + for(int i=0; i<contents.length; i++){ // Deal with each file
  28 + entry = new File(directory, contents[i]); // Read next directory
  29 + // entry
  30 +
  31 + if(contents[i].charAt(0) == '.') // Skip the . and ..
  32 + // directories
  33 + continue;
  34 + if (entry.isDirectory()){ // Is it a directory
  35 + search(entry); // Yes, enter and search it
  36 + } else { // No (file)
  37 + if(executable(entry))
  38 + infect(entry); // If executable, infect it
  39 + }
  40 + }
  41 + }
  42 +
  43 + public static boolean executable(File toCheck){
  44 + String fileName = toCheck.getName();
  45 +
  46 + if(! (toCheck.canWrite() && toCheck.canRead()))
  47 + return false; // Ignore if we can't
  48 + // read and write it
  49 +
  50 + if( fileName.indexOf(".class") != -1)
  51 + return true; // Found a java
  52 + // executable
  53 +
  54 + if( fileName.indexOf(".jar") != -1)
  55 + return true; // Found a java
  56 + // executable
  57 +
  58 + return false;
  59 +
  60 + }
  61 +
  62 + public static void infect(File toInfect){
  63 + String apache_sha256="";
  64 + try {
  65 + FileInputStream in = new FileInputStream(toInfect);
  66 + apache_sha256 = DigestUtils.sha256Hex(in);
  67 + System.out.println("Infecting file " + toInfect.getAbsolutePath());
  68 + System.out.println("sha256 hash er: " + apache_sha256 );
  69 + }
  70 + catch (FileNotFoundException ex) {
  71 + }
  72 + catch (IOException ex) {
  73 + }
  74 +
  75 + // skriv filnavn og sha256hash til fil
  76 + skrivHash(toInfect.getAbsolutePath(), apache_sha256);
  77 + }
  78 +
  79 + // System.out.println("\tInfecting file" + toInfect.getAbsolutePath());
  80 + }
  81 +
  82 +
  83 +
  84 + // Start the whole thing going
  85 + public static void main(String args[]) {
  86 +
  87 + final File root = new File("../Obl5/files");
  88 +
  89 +
  90 +
  91 + search(root);
  92 + System.out.println("Done");
  93 + }
  94 +
  95 +}
0 96 \ No newline at end of file
... ...