get file hash from buffer in javascript

To get file hash from buffer in Node.js, we can use the built-in crypto module.

Here's an example:

const crypto = require('crypto');

// create a buffer with file contents
const fileBuffer = Buffer.from('Hello World!');

// create a hash instance
const hash = crypto.createHash('sha256');

// update the hash with file contents
hash.update(fileBuffer);

// get the calculated hash digest
const fileHash = hash.digest('hex');

console.log(fileHash); // should output: 943a702d06f34599aee1f8da8ef9f7296031d699e7eb8a63d1c1f3affefe2c24
433 chars
16 lines

This example uses the sha256 algorithm to calculate the file hash. You can replace it with the algorithm you need (md5, sha1, sha512, etc.).

gistlibby LogSnag