Calculate the hash of Blob using JavaScript

Viewed 7399

I want to compare two Blobs to see if there are changes between them.

One way of doing this is by calculating the hash of the blobs and then comparing them, e.g.:

hash(firstBlob) === hash(secondBlob)

How can I calculate the hash of a Blob and check against another hash to see if they have changed?

2 Answers

I know this is kind of old but for someone looking for a better and newer solution please use the Crypto API and a SHA-256 or higher variant for the algorithm since MD5 has exploitable flaws.

var a = new FileReader();
a.readAsArrayBuffer(blob);
a.onloadend = function () {
  let hashPromise = crypto.subtle.digest("SHA-256", a.result);// it outputs a promise
};
Related