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?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With