Writing a service to verify the last modified time and the file tree structure

Since it is impractical to inspect every file to verify whether it has been tampered with, we'll only be verifying whether the file tree structure of the directory or the last modified time of any of the files has changed. This will indicate whether a file has been added or removed from our directory or any file has been tampered with since our app recorded the hashes. The individual files will be checked and verified only when we find that the MTH (MTH) has changed. We will check these files to locate the actual file(s) that were tampered with.

In this section, we'll write the /api/hashread service to check whether there is a mismatch between the current MTH (MTH) and the FTH (FTH) of the directory and the MTH and FTH captured in the blockchain by the /api/hashwrite service. In the next section, we'll write the /api/hashreadfile service to inspect and compare the hashes of the individual files in our directory.

Let's start writing the /api/hashread service:

  1. The request body that's sent to the /api/hashread service from the frontend contains the timestamp at which the MTH and FTH hashes were recorded in the blockchain and the directory path. The timestamp is under the  modtime key and the directory path is under the key DirPath, in the request body:
app.post("/api/hashread", function(request , response){

var modtime = request.body.modtime;
var jsonString;
var directory = request.body.DirPath;
var timestamp = [];
  1. Next, we call the walkDir method to get the current file tree structure of the directory and the last modified time of the files in the directory:
walkDir(directory,function(err,res){
if (res)
{
var files = res;
var counter = 1;
  1. Next, we iterate through the file list returned by the walkDir method:
files.forEach(function (file) { 
  1. For each iteration, we fetch the last modified time of the file using the fs.statSync method and push it into the timestamp array:
var stats = fs.statSync(file);
timestamp.push(stats.mtime);
  1. At the end of the iteration, we calculate the hash of the timestamp array with the last modified time (MTH) and the hash of the files array with the file tree structure (FTH):
var CounterT = counter;

if ( CounterT == files.length)
{
var FileTreeHash = new Keccak(256);
FileTreeHash.update(files.toString());
var FTH = FileTreeHash.digest('hex');
var MTimeHash = new Keccak(256);
MTimeHash.update(timestamp.toString());
var MTH = MTimeHash.digest('hex');
  1. The newly calculated MTH and FTH are sent as input parameters to the pRead() method, along with the modtime parameter. The pRead method will fetch the prerecorded hashes from the blockchain and compare them to see whether there is a mismatch. If there is a mismatch with the MTH, then it indicates that one or more files in the directory have been tampered with. If there is a mismatch with the FTH, then this indicates that a file has been added to or removed from the directory:
pRead(modtime, MTH, FTH).then(function(err,res){
if (res)
{
jsonString = JSON.stringify({result: res,files: files });
response.send(jsonString);
}
else
{
jsonString = JSON.stringify({result: err, files: files});
response.send(jsonString);
}
})}
counter++;
})}})});

The response from the pRead method (tampered/not tampered) that's received from the pRead function is sent to the response body in the result key and the files array is returned in the files key. After each iteration, the counter is incremented. 

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

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