Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

electron - how to generate file checksum

I have an electron app that will generate encrypted files. I want to provide to the user the checksum of the encrypted files to give the user the ability to check if the files aren't modified. How I can achive this with electron and node js api?

like image 468
newbiedev Avatar asked Dec 04 '25 17:12

newbiedev


1 Answers

Node.js has the inbuilt crypto library with a variety of different crypto algorothims

const crypto = require('crypto');
function getChecksum(path) {
    return new Promise((resolve, reject) => {
      // if absolutely necessary, use md5
      const hash = crypto.createHash('sha256');
      const input = fs.createReadStream(path);
      input.on('error', reject);
      input.on('data', (chunk) => {
          hash.update(chunk);
      });
      input.on('close', () => {
          resolve(hash.digest('hex'));
      });
    });
}

Example usage:

getChecksum('someFile.txt')
.then(checksum => console.log(`checksum is ${checksum}`))
.catch(err => console.log(err));
like image 148
t348575 Avatar answered Dec 06 '25 07:12

t348575



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!