Commit 3a501c75aa349cdae43887943653dc4be14e084c

Authored by prolly
1 parent dd4eb483

Laget meny. Alt ser ut til å funke.

Showing 2 changed files with 89 additions and 24 deletions   Show diff stats
hashfil.txt
1   -files\firstFolder\names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
2   -files\secondFolder\phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
3 0 \ No newline at end of file
src/hashObl5/ExecutableFiles.java
... ... @@ -5,6 +5,7 @@ import java.security.*;
5 5 import java.security.spec.X509EncodedKeySpec;
6 6 import java.util.HashMap;
7 7 import java.util.Map;
  8 +import java.util.Scanner;
8 9  
9 10 import org.apache.commons.codec.digest.DigestUtils;
10 11  
... ... @@ -214,10 +215,10 @@ public class ExecutableFiles extends Object {
214 215 bufin.close();
215 216  
216 217  
217   - boolean verifies = sig.verify(sigToVerify);
  218 + return sig.verify(sigToVerify);
218 219  
219 220  
220   - return true;
  221 +
221 222  
222 223 } catch (Exception e) {
223 224 System.err.println("Caught exception " + e.toString());
... ... @@ -281,6 +282,45 @@ public class ExecutableFiles extends Object {
281 282 return data;
282 283  
283 284 }
  285 + public static void checkSingleFile(File directory, String fileName, String hashFile) {
  286 + // executables
  287 +
  288 + File entry; // A reference to an entry
  289 + // in the directory
  290 + String entryName; // The full path name of a
  291 + // file
  292 +
  293 +
  294 +
  295 + if(directory == null) { return;} // Could not be opened;
  296 + // forget it
  297 +
  298 +
  299 + String contents[] = directory.list(); // Get an array of all the
  300 + // files in the directory
  301 + if(contents == null) return; // Could not access
  302 + // contents, skip it
  303 +
  304 + for(int i=0; i<contents.length; i++){ // Deal with each file
  305 + entry = new File(directory, contents[i]); // Read next directory
  306 + // entry
  307 +
  308 + if(contents[i].charAt(0) == '.') // Skip the . and ..
  309 + // directories
  310 + continue;
  311 + if (entry.isDirectory()){ // Is it a directory
  312 + checkSingleFile(entry,fileName,hashFile); // Yes, enter and search it
  313 + } else {
  314 + if(entry.getName().equals(fileName)) {
  315 + System.out.println("Do "+ hashFile+ " contain hash entry for "+ fileName+"? "+ verifisereHash(entry.getPath(),hashFile)); // No (file)
  316 + }
  317 + else {
  318 + System.out.println(fileName+" not found in "+directory.getPath());
  319 + }
  320 + // If executable, infect it
  321 +}
  322 +}
  323 + }
284 324 public static void checkFiles(File directory, String hashStoragePath) {
285 325 // executables
286 326  
... ... @@ -310,34 +350,61 @@ public class ExecutableFiles extends Object {
310 350 if (entry.isDirectory()){ // Is it a directory
311 351 checkFiles(entry,hashStoragePath); // Yes, enter and search it
312 352 } else { // No (file)
313   - System.out.println("Does " +entry.getName()+"in"+entry.getPath()+ " checksum matches the expected one? " +verifisereHash(entry.getPath(),hashStoragePath)); // If executable, infect it
  353 + System.out.println("Does " +entry.getName()+" in "+entry.getPath()+ " checksum matches the expected one? " +verifisereHash(entry.getPath(),hashStoragePath)); // If executable, infect it
314 354 }
315 355 }
316 356 }
317 357  
318 358 public static void main(String args[]) {
319   - boolean testg=false;
320   - boolean testv=false;
321   -
  359 + Scanner input=new Scanner(System.in);
322 360 final File root = new File("files");
  361 + boolean testg=false;
  362 + boolean testv=false;
  363 + boolean run=true;
  364 + int yourChoice;
  365 + String fileName="";
  366 + 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? ");
  368 + yourChoice=input.nextInt();
  369 + switch(yourChoice) {
  370 + case 1:
  371 + search(root);
  372 + break;
  373 + case 2:
  374 + testg=genSig("test","hashfil.txt","testing","signedhash","pkey");
  375 +
  376 + if(testg) {
  377 +
  378 + System.out.println("Hashfile signed!");
  379 + }else{
  380 + System.out.println("Failed to generate signature");
  381 + }
  382 + break;
  383 + case 3:
  384 + System.out.println("Enter filename: ");
  385 + fileName=input.next();
  386 + checkSingleFile(root,fileName, "hashfil.txt");
  387 + break;
  388 + case 4:
  389 + checkFiles(root,"hashfil.txt");
  390 + break;
  391 + case 5:
  392 + testv=verSig("pkey","signedhash","hashfil.txt");
  393 + if(testv) {
  394 + System.out.println("Hashfile verified!");
  395 + }
  396 + else {
  397 + System.out.println("Warning! Failed to verify hashfile. Hashfile has been modified!");
  398 + }
  399 + break;
  400 + case 6:
  401 + run=false;
  402 + System.out.println("Exiting..");
  403 + break;
  404 + }
323 405  
324   - search(root);
325   - testg=genSig("test","hashfil.txt","testing","signedhash","pkey");
326 406  
327   - if(testg) {
328   -
329   - System.out.println("Hashfile signed!");
330   - }else{
331   - System.out.println("Failed to generate signature");
332   - }
333   - checkFiles(root,"hashfil.txt");
334   - testv=verSig("pkey","signedhash","hashfil.txt");
335   - if(testv) {
336   - System.out.println("Hashfile verified!");
337   - }
338   - else {
339   - System.out.println("Warning! Failed to verify hashfile.");
340   - }
341 407  
342 408 }
343 409 }
  410 +}
344 411 \ No newline at end of file
... ...