hashreadfilecryptojs

how to Calculate SHA3-256 for every file in js?


I have many data file inside data folder . i just read one of them which is returning 64 hex code smth like that f0bef0fedb9a235b0b559f2749f9e6ce6405761b784747be51041c6610232896. How can i loop through each file inside data folder and return 64 hex code? and also there some other requirements: Write hashes as 64 hex digits in lower case. Sort hashes as strings. Join sorted hashes without any separator. Concatenate resulted string with your e-mail. Here is my starting code :

import crypto from 'crypto'
import fs from 'fs'
const fileBuffer = fs.readFileSync('./data/file_00.data')
console.log(fileBuffer);
const hash = crypto.createHash("SHA3-256")
const finalHex = hash.update(fileBuffer).digest("hex")
console.log(finalHex);

Solution

  • const fileNames = fs.readdirSync('./data/');
    for(const fName of fileNames) {
       const fileBuffer = fs.readFileSync(`./data/${fName}`);
       //Calculate hash for every file here.
    }