Writing a backend service for securing a directory by recording hashes in the blockchain

The /api/hashwrite service performs the very important task of calculating the hash of each individual file in a directory, the hash of the last modified time of all the files in the directory, and the hash of the file tree of the directory, and then storing them in the blockchain. It takes the directory path as an input parameter.

The following are the steps that are carried out by the service:

  1. Extract the directory path to be secured from the request body.
  2. Call the walkDir function to list all the files in the directory.
  3. Iterate through all the files in the directory, calculate the hash for each file, and call the bWrite method to write the hash to the blockchain, indexed by the hash of the directory path of the file. For each iteration created, add the last modified time of the file to an array object and the file path to an array object.
  4. At the end of the iteration, call the pWrite function to write the hash of the arrays with the last modified time and file path to the blockchain indexed by the current timestamp.
  5. Return the array of the list of files secured by the blockchain, the last MTH and FTH, and the timestamp at which the hashes were recorded in the response object to the requester.

Let's start writing the service:

  1. The request body to the service has the directory path that is to be secured under the  DirPath key:
app.post("/api/hashwrite", function(request , response){
var directory = request.body.DirPath;
var timestamp = [];
var modtime;
var jsonString;
  1. We store the directory path in the directory variable.
  2. Next, we call the walkDir method. The directory variable is sent as an input parameter:
walkDir(directory,function(err,res){
if (res)
{
var files = res;
  1. The walkDir method returns the res array with the list of files in our directory path. Upon a successful response from walkDir, we iterate through all the members of the files array, this is basically all the directory paths for all the files in our directory:
 var counter = 1;
files.forEach(function (file) {
  1. For each file in our array, we calculate the hash of the file path and the file data.
  2. First, we calculate the SHA3 hash digest of the file path and store it in the p variable:
var hashPath = new Keccak(256);
hashPath.update(file);
var p = hashPath.digest('hex');
  1. Next, we fetch the last modified time of the file using the fs statSync method and store it in the timestamp array. The timestamp array will store the last modified time for all the files:
var stats = fs.statSync(file);
timestamp.push(stats.mtime);
  1. Lastly, we use the fs ReadStream method to read the file and calculate the SHA3 hash digest of the file's contents. The hash of the file's contents is stored in the d variable:
var hashFile = new Keccak(256);
var s = fs.ReadStream(file);
s.on('data', function(d) { hashFile.update(d);});
s.on('end', function() {
var d = hashFile.digest('hex');

console.log("File path hash",p);
console.log("File Hash",d);

bWrite(p,d);
  1. At the end of the hash digest calculation the file, the hash of the file path and the file's contents are printed to the console.
  1. We call the bWrite method with the input parameters p and d to write the file's hash to the blockchain, indexed by the file path hash.
  1. When we reach the end of the file list in our directory, we calculate the FTH (FTH) using the files array and the MTH (MTH) using the timestamp array. The file list, MTH, and FTH are printed to the console for reference:
var CounterT = counter;
if ( CounterT == files.length)
{
console.log("Files",files);
var FileTreeHash = new Keccak(256);
FileTreeHash.update(files.toString());

var FTH = FileTreeHash.digest('hex');
console.log("FTH ");
console.log(FTH);

var MTimeHash = new Keccak(256);
MTimeHash.update(timestamp.toString());
var MTH = MTimeHash.digest('hex');
console.log("MTH ");
console.log(MTH);
  1. The pWrite method is called to write the hashes, MTH and FTH, to the blockchain. A JSON response is sent to the requester with the files array, MTH, FTH, and the time at which the hash was recorded (modtime):
pWrite(FTH,MTH).then(function(modtime){
jsonString = JSON.stringify({files: files,MTH: MTH,FTH: FTH, modtime: modtime});
response.send(jsonString);
});
}
});

counter++;
});
  1. The counter is incremented at the end of each iteration.
  1. An else block catches any errors that occurred while fetching the list of files using walkDir():
else
{
console.log(err);
jsonString = JSON.stringify({Error: err});
response.send(jsonString);
}
});
})

That ends our walkthrough of the /api/hashwrite service. This service is called the first time our app is launched. It asks the user for the directory to be secured and sends it to the service as a request. Next, we'll write the services that are used to read the hashes that are recorded in the blockchain and compare them with the real-time hash signatures of files, the real-time hash of the modified time of the files and the real-time file tree structure hash. In the case of a mismatch, it indicates that the files and the directory have been tampered with.

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

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