/*
implements the command line interface and handles user input
author: mm
*/
import java.util.Scanner;
public class Driver {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
System.out.println("Please enter target directory:");
String root = reader.nextLine();
System.out.println("Target directory set to " + root);
int option = -1; // menu option
while (true) {
System.out.println("Please select an option:");
System.out.println("1. Create new snapshot");
System.out.println("2. Create report");
System.out.println("3. Exit");
int choice = reader.nextInt();
// make sure choice is in valid range
if (choice >= 1 && choice <= 3) {
if (choice == 1) {
System.out.println("Scanning " + root);
FileScan s = new FileScan(root);
FileHash f = new FileHash();
s.scan(root, true); // scan target and create snapshot file
System.out.print("Snapshot created: ");
// hash the snapshot file and display to user
try {
String hash = f.hashFile("snapshot");
System.out.println(hash);
} catch (Exception e) {
e.printStackTrace();
}
} else if (choice == 2) {
FileHash f = new FileHash();
// hash the snapshot file and display to user
try {
String hash = f.hashFile("snapshot");
System.out.println("Snapshot hash value: " + hash);
} catch (Exception e) {
e.printStackTrace();
}
Monitor m = new Monitor(root);
System.out.println("Creating report for " + root);
m.check();
System.out.println("Report created");
} else {
break;
}
} else {
System.out.println("Please enter correct number");
}
}
}
}
/*
Implement the recursive traversal and enumeration of the target directory and also the generation of the snapshot file.
author: mm
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class FileScan {
public String root = "";
public ArrayList<String> listOfFiles = new ArrayList<String>();
public ArrayList<String> listOfDirs = new ArrayList<String>();
public FileHash fh = new FileHash();
public String fileData = "";
public FileScan(String root) {
this.root = root;
}
public static void main(String args[]) {
FileScan s = new FileScan(""); // set root directory here
try {
s.scan(s.getRoot(), true);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("test " + s.fileData);
// System.out.println("Files:");
// for (int i = 0; i < s.listOfFiles.size(); i++) {
// System.out.print(s.listOfFiles.get(i) + " ");
// }
//
// System.out.println();
// System.out.println("Dirs:");
// for (int i = 0; i < s.listOfDirs.size(); i++) {
// System.out.print(s.listOfDirs.get(i) + " ");
// }
}
/*
* getters and setter
*/
public String getRoot() {
return root;
}
public void setRoot(String root) {
this.root = root;
}
public ArrayList<String> getListOfFiles() {
return listOfFiles;
}
public void setListOfFiles(ArrayList<String> listOfFiles) {
this.listOfFiles = listOfFiles;
}
public ArrayList<String> getListOfDirs() {
return listOfDirs;
}
public void setListOfDirs(ArrayList<String> listOfDirs) {
this.listOfDirs = listOfDirs;
}
public void getFiles(String path) throws Exception {
File currentDir = new File(path);
File[] all = currentDir.listFiles();
for (int i = 0; i < all.length; i++) {
if (all[i].isFile()) {
this.listOfFiles.add(all[i].getName());
} else if (all[i].isDirectory()) {
this.listOfDirs.add(all[i].getName());
} else {
// not a file or directory
}
}
}
/*
* helper function for scanDir method input: directory absolute path (string),
* option to output file and hash data as snapshot file (boolean) output: string
* representation of snapshot file (string)
*/
public String scan(String path, boolean outputToFile) {
this.fileData = "";
try {
this.scanDir(path);
} catch (Exception e) {
e.printStackTrace();
}
if (outputToFile) {
File out = new File("snapshot");
try (FileOutputStream fout = new FileOutputStream(out)) {
// create file if there is none
if (!out.exists()) {
out.createNewFile();
}
byte[] dataBytes = fileData.getBytes();
fout.write(dataBytes);
fout.flush();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return this.fileData;
}
/*
* scans a target directory and subdirectories and stores file and hash values
* as string input: absolute path of target directory (string) output: appends
* each file to fileData String
*/
public void scanDir(String path) throws Exception {
File currentDir = new File(path);
fileData += currentDir.getAbsoluteFile() + "|";
if (currentDir.isFile()) {
fileData += fh.hashFile(currentDir);
} else {
fileData += "directory";
}
fileData += "\n";
if (currentDir.isDirectory()) {
String[] items = currentDir.list();
for (String name : items) {
scanDir(path + "/" + name);
}
}
}
}
/*
Implements secure hashing of files with SHA-256
author: mm
*/
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
public class FileHash {
public static void main(String args[]) {
FileHash fh = new FileHash();
File f = new File("remove_crw.cmd");
try {
System.out.println(fh.hashFile(f));
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("hello world");
}
/*
* gives the SHA-256 hash given the absolute path to a file inputs: absolute
* path of file (string) output: SHA-256 hash of file (string)
*/
public String hashFile(String fileName) throws Exception {
MessageDigest m = MessageDigest.getInstance("SHA-256");
FileInputStream f = new FileInputStream(fileName);
byte[] fileBytes = new byte[1024];
int count = 0;
// read bytes from file
while ((count = f.read(fileBytes)) != -1) {
m.update(fileBytes, 0, count);
}
// get hash of bytes
byte[] hashBytes = m.digest();
// convert to hex and represent as string
StringBuilder s = new StringBuilder("");
for (int i = 0; i < hashBytes.length; i++) {
int temp = (hashBytes[i] & 0xff) + 0x100;
s.append(Integer.toString(temp, 16).substring(1));
}
return s.toString();
}
/*
* gives the SHA-256 hash given a file object inputs: file (File) output:
* SHA-256 hash of file (string)
*/
public String hashFile(File f) throws Exception {
MessageDigest m = MessageDigest.getInstance("SHA-256");
FileInputStream fis = new FileInputStream(f);
byte[] fileBytes = new byte[1024];
int count = 0;
// read bytes from file
while ((count = fis.read(fileBytes)) != -1) {
m.update(fileBytes, 0, count);
}
// get hash of bytes
byte[] hashBytes = m.digest();
// convert to hex and represent as string
StringBuilder s = new StringBuilder("");
for (int i = 0; i < hashBytes.length; i++) {
int temp = (hashBytes[i] & 0xff) + 0x100;
s.append(Integer.toString(temp, 16).substring(1));
}
return s.toString();
}
}
/*
Defines the functionality of reading the snapshot file and comparing it to the current state of the target directory
author: mm
*/
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
public class Monitor {
public HashMap<String, String> snapshotHashMap = new HashMap<String, String>();
public HashMap<String, String> currentHashMap = new HashMap<String, String>();
public String root = "";
public Monitor(String root) {
this.root = root;
}
public static void main(String args[]) {
Monitor m = new Monitor(""); // set root directory here
m.check();
}
/*
* compares the old state of dir with new state by comparing data stored in hash
* tables input: currentHashMap and snapshotHashMap output: writes report.txt to
* project directory
*/
public void compare() {
String diff = "";
Iterator<String> itr1 = snapshotHashMap.keySet().iterator();
while (itr1.hasNext()) {
// get directory/file absolute path
String s1 = itr1.next();
if (currentHashMap.containsKey(s1)) {
// both hash maps contain same file or dir
if (!snapshotHashMap.get(s1).equals(currentHashMap.get(s1))) {
// file has been modified
diff += "change: " + s1 + ", " + snapshotHashMap.get(s1) + " -> " + currentHashMap.get(s1) + "\n";
}
// remove matches from both hash maps
itr1.remove();
currentHashMap.remove(s1);
}
}
// add remaining items to log
Iterator<String> itr2 = snapshotHashMap.keySet().iterator();
while (itr2.hasNext()) {
String s2 = itr2.next();
diff += "deleted: " + s2 + ", " + snapshotHashMap.get(s2) + "\n";
}
Iterator<String> itr3 = currentHashMap.keySet().iterator();
while (itr3.hasNext()) {
String s3 = itr3.next();
diff += "added: " + s3 + ", " + currentHashMap.get(s3) + "\n";
}
// write log to file report.txt
File out = new File("report.txt");
try (FileOutputStream fout = new FileOutputStream(out)) {
// create file if there is none
if (!out.exists()) {
out.createNewFile();
}
byte[] dataBytes = diff.getBytes();
fout.write(dataBytes);
fout.flush();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* helper function that reads data from snapshot file and scans current dir.
* Writes data to hash maps input: snapshot file output: writes old and current
* state to hash map
*/
public void check() {
snapshotHashMap.clear();
currentHashMap.clear();
// read snapshot file
File snapshotFile = new File("snapshot");
if (snapshotFile.exists()) {
try (BufferedReader b = new BufferedReader(new FileReader(snapshotFile))) {
String row;
while ((row = b.readLine()) != null) {
// read line by line and delimit by ,
System.out.println("snapshot:"+row);
String[] split = row.split("\\|");
snapshotHashMap.put(split[0], split[1]);
System.out.println(split[0]);
System.out.println(split[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
// scan current directory
FileScan s = new FileScan(this.root);
String newData = s.scan(s.getRoot(), false);
try (BufferedReader b = new BufferedReader(new StringReader(newData))) {
String row;
while ((row = b.readLine()) != null) {
System.out.println(row);
String[] split = row.split("\\|");
currentHashMap.put(split[0], split[1]);
System.out.println(split[0]);
System.out.println(split[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("No snapshot file exists");
}
this.compare();
}
}
/*implements the command line interface and handles user inputauthor: mm */import java.util.Scanner;public class Driver { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("Please enter targe.