Commit 6809e347eb03f56eb2831c1ca503f46427fb6d07

Authored by Maryna German Bjørnstrøm
1 parent 3a501c75

Del4. Det skal være mulighet for å verifisere alle filer i en katalog

med underkataloger mot tidligere lagrede hash verdier.
Litt endring i meny.
clearHashDB for rydde alt innhold i hashfil.txt
.classpath
1   -<?xml version="1.0" encoding="UTF-8"?>
2   -<classpath>
3   - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4   - <classpathentry kind="src" path="src"/>
5   - <classpathentry kind="lib" path="C:/Users/paul/eclipse-workspace/commons-codec-1.11/commons-codec-1.11.jar"/>
6   - <classpathentry kind="output" path="bin"/>
7   -</classpath>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
  4 + <classpathentry kind="src" path="src"/>
  5 + <classpathentry kind="lib" path="/Users/margerbjo/git/Oblig5/commons-codec-1.11.jar"/>
  6 + <classpathentry kind="output" path="bin"/>
  7 +</classpath>
... ...
test renamed to files/firstFolder/test.txt
No preview for this file type
hashfil.txt
  1 +files/secondFolder/phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723files/firstFolder/names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
  2 +files/firstFolder/testFile.txt,a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
0 3 \ No newline at end of file
... ...
referanselisten.txt
1 1 https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html
2 2 http://kark.hin.no/opsys/tanenbaum/java/ExecutableFiles.java
3   -http://kark.hin.no/sikkerhet/filehash/oblig5_tips.html
4 3 \ No newline at end of file
  4 +http://kark.hin.no/sikkerhet/filehash/oblig5_tips.html
  5 +https://stackoverflow.com/questions/6994518/how-to-delete-the-content-of-text-file-without-deleting-itself
5 6 \ No newline at end of file
... ...
signedhash
No preview for this file type
src/hashObl5/ExecutableFiles.java
... ... @@ -82,15 +82,16 @@ public class ExecutableFiles extends Object {
82 82 skrivHash(toInfect.getPath(), apache_sha256);
83 83 }
84 84  
85   -
  85 + // Created by Maryna German Bjørnstrøm(del3)
86 86 private static void skrivHash(String absolutePath, String apache_sha256) {
87 87 String fileName = "hashfil.txt";
88 88 StringBuilder data = new StringBuilder();
89 89 String line;
90 90  
91 91 try {
92   -
  92 +
93 93 FileReader fileReader = new FileReader(fileName);
  94 +
94 95  
95 96 BufferedReader bufferedReader = new BufferedReader(fileReader);
96 97  
... ... @@ -115,6 +116,23 @@ public class ExecutableFiles extends Object {
115 116 }
116 117 }
117 118  
  119 + private static void clearHashDB() {
  120 + String fileName = "hashfil.txt";
  121 + File file = new File(fileName);
  122 +
  123 + if(file.exists()) {
  124 + PrintWriter writer; // https://stackoverflow.com/questions/6994518/how-to-delete-the-content-of-text-file-without-deleting-itself
  125 + try {
  126 + writer = new PrintWriter(file);
  127 + writer.print("");
  128 + writer.close();
  129 +
  130 + } catch (FileNotFoundException e) {
  131 + e.printStackTrace();
  132 + }
  133 +
  134 + }
  135 + }
118 136 private static boolean genSig(String ksPath, String filePath, String ksPass, String sig, String keyname ) {
119 137 char[] spass=ksPass.toCharArray();
120 138  
... ... @@ -214,11 +232,7 @@ public class ExecutableFiles extends Object {
214 232  
215 233 bufin.close();
216 234  
217   -
218 235 return sig.verify(sigToVerify);
219   -
220   -
221   -
222 236  
223 237 } catch (Exception e) {
224 238 System.err.println("Caught exception " + e.toString());
... ... @@ -226,7 +240,7 @@ public class ExecutableFiles extends Object {
226 240 }
227 241 }
228 242  
229   -
  243 + // Created by Maryna German Bjørnstrøm(del3)
230 244 private static boolean verifisereHash(String filePath, String hashStoragePath) {
231 245 Map<String, String> storage = parseStorage(hashStoragePath);
232 246 String apache_sha256="";
... ... @@ -251,7 +265,7 @@ public class ExecutableFiles extends Object {
251 265 return false;
252 266  
253 267 }
254   -
  268 + // Created by Maryna German Bjørnstrøm(del3)
255 269 private static Map<String, String> parseStorage(String storage){
256 270 HashMap<String, String> data = new HashMap<>();
257 271  
... ... @@ -282,6 +296,36 @@ public class ExecutableFiles extends Object {
282 296 return data;
283 297  
284 298 }
  299 +
  300 + // Created by Maryna German Bjørnstrøm(del3)
  301 + private static void verifisereHashToAll(File root) {
  302 +
  303 + File entry;
  304 + String entryName;
  305 +
  306 + if(root == null) { return;}
  307 + String contents[] = root.list();
  308 +
  309 + if(contents == null) return;
  310 +
  311 + for(int i=0; i<contents.length; i++){
  312 + entry = new File(root, contents[i]);
  313 +
  314 + if(contents[i].charAt(0) == '.')
  315 +
  316 + continue;
  317 + if (entry.isDirectory()){
  318 + verifisereHashToAll(entry);
  319 + } else {
  320 + if(verifisereHash(entry.getPath(), "hashfil.txt")){
  321 + System.out.println("File " + entry.getPath() + " wasn't changed.");
  322 + } else {
  323 + System.out.println("File " + entry.getPath() + " was changed.");
  324 + }
  325 + }
  326 + }
  327 + }
  328 +
285 329 public static void checkSingleFile(File directory, String fileName, String hashFile) {
286 330 // executables
287 331  
... ... @@ -312,7 +356,8 @@ public class ExecutableFiles extends Object {
312 356 checkSingleFile(entry,fileName,hashFile); // Yes, enter and search it
313 357 } else {
314 358 if(entry.getName().equals(fileName)) {
315   - System.out.println("Do "+ hashFile+ " contain hash entry for "+ fileName+"? "+ verifisereHash(entry.getPath(),hashFile)); // No (file)
  359 + System.out.println("Do "+ hashFile+ " contain hash entry for "+ fileName+"? "
  360 + + verifisereHash(entry.getPath(),hashFile)); // No (file)
316 361 }
317 362 else {
318 363 System.out.println(fileName+" not found in "+directory.getPath());
... ... @@ -350,9 +395,10 @@ public class ExecutableFiles extends Object {
350 395 if (entry.isDirectory()){ // Is it a directory
351 396 checkFiles(entry,hashStoragePath); // Yes, enter and search it
352 397 } else { // No (file)
353   - System.out.println("Does " +entry.getName()+" in "+entry.getPath()+ " checksum matches the expected one? " +verifisereHash(entry.getPath(),hashStoragePath)); // If executable, infect it
354   -}
355   -}
  398 + System.out.println("Does " +entry.getName()+" in "+entry.getPath()+ " checksum matches the expected one? "
  399 + +verifisereHash(entry.getPath(),hashStoragePath)); // If executable, infect it
  400 + }
  401 + }
356 402 }
357 403  
358 404 public static void main(String args[]) {
... ... @@ -363,48 +409,81 @@ public class ExecutableFiles extends Object {
363 409 boolean run=true;
364 410 int yourChoice;
365 411 String fileName="";
  412 + String pathToFile;
  413 +
366 414 while(run) {
367   - System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "1: Generate hashkeys for files","2: Sign hashfile", "3: Check hash for single file ","4. Check hash for all files","5: Verify signature","6: Exit program","Your choice? ");
  415 + System.out.printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
  416 + "1: Generate hashkeys for files",
  417 + "2: Sign hashfile",
  418 + "3: Check if path exist for single file ",
  419 + "4: Check hash for single file ",
  420 + "5: Check hash for all files ",
  421 + "6. Check if path exist for all files",
  422 + "7: Verify signature",
  423 + "8: Exit program","Your choice: ");
368 424 yourChoice=input.nextInt();
369 425 switch(yourChoice) {
370 426 case 1:
  427 + System.out.println();
  428 + clearHashDB();
371 429 search(root);
  430 +
372 431 break;
  432 +
373 433 case 2:
  434 + System.out.println();
374 435 testg=genSig("test","hashfil.txt","testing","signedhash","pkey");
375 436  
376   - if(testg) {
377   -
  437 + if(testg){
378 438 System.out.println("Hashfile signed!");
379 439 }else{
380 440 System.out.println("Failed to generate signature");
381 441 }
382 442 break;
  443 +
383 444 case 3:
  445 + System.out.println();
384 446 System.out.println("Enter filename: ");
385 447 fileName=input.next();
386 448 checkSingleFile(root,fileName, "hashfil.txt");
387 449 break;
  450 +
388 451 case 4:
389   - checkFiles(root,"hashfil.txt");
  452 + System.out.println();
  453 + System.out.print("Does the file's hash value matches the expected ? ");
  454 + pathToFile = input.next();
  455 + System.out.println(verifisereHash(pathToFile, "hashfil.txt"));
390 456 break;
  457 +
391 458 case 5:
  459 + System.out.println();
  460 + System.out.println("Does the file's hash value matches expected all files? ");
  461 + String folder = input.next();
  462 + File rootFolder = new File(folder);
  463 + verifisereHashToAll(rootFolder);
  464 + break;
  465 +
  466 + case 6:
  467 + System.out.println();
  468 + checkFiles(root,"hashfil.txt");
  469 + break;
  470 +
  471 + case 7:
  472 + System.out.println();
392 473 testv=verSig("pkey","signedhash","hashfil.txt");
393   - if(testv) {
  474 + if(testv){
394 475 System.out.println("Hashfile verified!");
395   - }
396   - else {
  476 + }else{
397 477 System.out.println("Warning! Failed to verify hashfile. Hashfile has been modified!");
398 478 }
399 479 break;
400   - case 6:
  480 +
  481 + case 8:
  482 + System.out.println();
401 483 run=false;
402 484 System.out.println("Exiting..");
403 485 break;
  486 + }
  487 + }
404 488 }
405   -
406   -
407   -
408   - }
409   - }
410   -}
411 489 \ No newline at end of file
  490 + }
412 491 \ No newline at end of file
... ...