Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.vvzorichev.hash;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         switch (args[0]) {         
  6.            
  7.             case "-i":
  8.                 InteractiveMode imobj = new InteractiveMode(args);
  9.                 break;
  10.  
  11.             default:
  12.                 SimpleMode smobj = new SimpleMode(args);
  13.                 break;
  14.         }
  15.     }
  16. }
  17.  
  18. package com.vvzorichev.hash;
  19.  
  20. import java.io.IOException;
  21. import java.util.Scanner;
  22.  
  23. public class InteractiveMode {
  24.  
  25.     private String Args[];
  26.     private boolean Stop;
  27.  
  28.     public InteractiveMode(String input[]) {
  29.         this.Args = input;
  30.         this.Stop = false;
  31.     }
  32.  
  33.     private void PrintHash(String fileName) {
  34.         try
  35.         {
  36.             CountHash chObj = new CountHash(fileName);
  37.             String md5 = chObj.CountMd5();
  38.             String sha256 = chObj.CountSha256();
  39.             System.out.println("\nFile : " + fileName + "\nMd5 : " + md5 + "\nSha256 : " + sha256 + "\n");
  40.         }
  41.         catch (IOException ex)
  42.         {
  43.             System.out.println("File does not found!\n");
  44.         }
  45.     }
  46.  
  47.     public void Interactive() {
  48.         while (!stop) {
  49.             System.out.println("Enter file / files names:");
  50.             Scanner input = new Scanner(System.in);
  51.             for (String str : input.nextLine().split(" ")) {
  52.                 printHash(str);
  53.             }
  54.             boolean answer = false;
  55.             while (!answer) {
  56.                 System.out.println("Do you want to count hash of another files?\n1 - Yes\n2 - No");
  57.                 String response = input.next();
  58.                 switch(response){
  59.  
  60.                     case "2":
  61.                         stop = true;
  62.                         answer = true;
  63.                         break;
  64.  
  65.                     case "1":
  66.                         answer = true;
  67.                         break;
  68.                    
  69.                     default:
  70.                         System.out.println("Incorrect answer! Try again");
  71.                         break;
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. package com.vvzorichev.hash;
  79.  
  80. import org.apache.commons.codec.digest.DigestUtils;
  81.  
  82. import java.io.FileInputStream;
  83. import java.io.IOException;
  84.  
  85. public class CountHash {
  86.  
  87.     private String fileName;
  88.  
  89.     public CountHash(String str) {
  90.         this.fileName = str;
  91.     }
  92.  
  93.     public String CountMd5() throws IOException {
  94.         return DigestUtils.md5Hex(new FileInputStream(fileName));
  95.     }
  96.  
  97.     public String CountSha256() throws IOException {       
  98.         return DigestUtils.sha256Hex(new FileInputStream(fileName));
  99.     }
  100. }
  101.  
  102. package com.vvzorichev.hash;
  103.  
  104. import java.io.IOException;
  105.  
  106. public class SimpleMode {
  107.  
  108.     private String Args[];
  109.     public String HashType;
  110.  
  111.     public SimpleMode(String input[]) {
  112.         this.Args = input;
  113.         this.HashType = Args[0];
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement