计算Blob的MD5哈希值

时间:2022-10-28 18:10:49

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

如何计算Blob的MD5哈希并检查另一个哈希以查看它们是否已更改?

EDIT: I'm currently using CryptoJS

编辑:我目前正在使用CryptoJS

1 个解决方案

#1


4  

You can use the FileReader API to get the contents of the blob for comparison. If you have to use CryptoJS for this, you can use readAsBinaryString:

您可以使用FileReader API获取blob的内容以进行比较。如果你必须使用CryptoJS,你可以使用readAsBinaryString:

var a = new FileReader();
a.readAsBinaryString(blob);
a.onloadend = function () {
  console.log(CryptoJS.MD5(CryptoJS.enc.Latin1.parse(a.result)));
};

Note that readAsBinaryString is deprecated, so if you can use another library, such as SparkMD5, you could use an array buffer instead:

请注意,不推荐使用readAsBinaryString,因此如果您可以使用其他库(如SparkMD5),则可以使用数组缓冲区:

var a = new FileReader();
a.readAsArrayBuffer(blob);
a.onloadend = function () {
  console.log(SparkMD5.ArrayBuffer.hash(a.result));
};

#1


4  

You can use the FileReader API to get the contents of the blob for comparison. If you have to use CryptoJS for this, you can use readAsBinaryString:

您可以使用FileReader API获取blob的内容以进行比较。如果你必须使用CryptoJS,你可以使用readAsBinaryString:

var a = new FileReader();
a.readAsBinaryString(blob);
a.onloadend = function () {
  console.log(CryptoJS.MD5(CryptoJS.enc.Latin1.parse(a.result)));
};

Note that readAsBinaryString is deprecated, so if you can use another library, such as SparkMD5, you could use an array buffer instead:

请注意,不推荐使用readAsBinaryString,因此如果您可以使用其他库(如SparkMD5),则可以使用数组缓冲区:

var a = new FileReader();
a.readAsArrayBuffer(blob);
a.onloadend = function () {
  console.log(SparkMD5.ArrayBuffer.hash(a.result));
};