Creating a method to check for a mismatch between the last modified time and the file tree structure

Next, we will look at the pathCheck() method. After writing the hash signatures to the blockchain, the app renders the FolderBlock component. This component triggers the 5-second interval timer by calling the startTimer method. The startTimer method, in turn, calls the pathCheck method, every 5 seconds to verify whether there is a mismatch between the hash signature of the MTH (MTH) and FTH (FTH) written in the blockchain and the actual hash value of the MTH and FTH when calculated from the directory under observation. A mismatch indicates that some data in our directory has been tampered with.

Let's see how the pathCheck method achieves this:

  1. We start by capturing the app state and the  DirPath state variable(directory path),  modtime (the time when the hashes were captured in the blockchain), and files locally (the files array with the list of files being observed):
pathCheck = () =>{ 

let app = this;

var DirPath = this.state.fields.DirPath;
var modtime = this.state.modtime;
var files = this.state.files;

this.setState({
status: 'Verifying'
});
  1. We also change the status variable to Verifying.
  2. Next, we call the hashread service on our backend server. Remember that the hashread service takes DirPath and modtime as input parameters so that it can fetch the MTH and FTH recorded in the blockchain, calculate the present values of MTH and FTH from our directory, and see whether there is a mismatch between the two. A mismatch indicates that one or more files have been tampered with or have been added/removed.
  3. We then use fetch to call the hashread service on port 3602 of our backend server and provide the DirPath and modtime input parameters in the request body:
fetch('http://localhost:3602/api/hashread', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},

body: JSON.stringify({
DirPath: DirPath,
modtime: modtime,
})
}).then(function(response,error) {
  1. Upon a successful response from the backend, we parse the data in the response.json:
if(response)
{
return response.json();
}
else
{
console.log(error);
}
}).then(function(data){
  1. The result of the check (Tampered/Not Tampered) is stored in the fstatus state variable, where it can be accessed by the other components of the app. The list of files currently present in the directory is returned by the hashread service in the response key files. We store these in the filesNew array:
then(function(data){
app.setState({
fstatus: data.result,
});
var filesNew = data.files;
if(data.result == "Tampered")
{
clearInterval(app.intervalHandle);

var params = {files,filesNew};
app.compFiles(params);
}
});
}

Next, we check whether the status returned by the backend service is Tampered. If the directory has been tampered with, we stop the iterating interval and call the compFiles method with the parameters files and filesNew (the original list of files in the directory and the list of files currently in the directory). The compFile method will allow us to identify whether any files have been added or removed from the directory. This wraps up our pathCheck method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.39.32