小编Jür*_*gen的帖子

使用 crypto.subtle.digest("SHA-256", buffer) 对大文件进行哈希处理

我开发了一个网络应用程序,用户可以通过输入字段选择多个文件。然后通过以下代码计算 sha-256 校验和。该代码(取自developer.mozilla.org)仅适用于小文件。我还需要更改什么才能处理大文件(例如 1GB+)?

function sha256(buffer){
  return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
    return hex(hash);
  });
}

function hex(buffer) {
  var hexCodes = [];
  var view = new DataView(buffer);
  for (var i = 0; i < view.byteLength; i += 4) {
    // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
    var value = view.getUint32(i)
    // toString(16) will give the hex representation of the number without padding
    var stringValue = value.toString(16)
    // We use concatenation and slice …
Run Code Online (Sandbox Code Playgroud)

javascript hash cryptography digest

6
推荐指数
0
解决办法
1822
查看次数

标签 统计

cryptography ×1

digest ×1

hash ×1

javascript ×1