Hash large files with crypto.subtle.digest("SHA-256", buffer)
Asked Answered
C

0

6

i have developed a web application where a user can select multiple files via a input field. Then the sha-256 checksums are calculated by the following code. The code (taken from developer.mozilla.org) only works for small files. What do I have to change to handle large files (e.g. 1GB+) too?

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 for padding
    var padding = '00000000'
    var paddedValue = (padding + stringValue).slice(-padding.length)
    hexCodes.push(paddedValue);
  }

  // Join all the hex strings into one
  return hexCodes.join("");
}
Cabezon answered 10/10, 2018 at 10:21 Comment(8)
does this internal digest to your application or some other parties will test it?Trustful
the function sha256 takes an ArrayBuffer as argument and returns a string (sha256-hash-string). In my application i read a file as ArrayBuffer then i call the sha256 from that and alert the resulting hash. with small files a alert box opens and shows the correct hash but with large files there is no alert box.Adjectival
I look for the limitation but couldn't find. One must have looked a the implementation. If the digest is not going to be used outside of your application than I can give a simple solution to you. If you aim to send the digest to third parties to be checked no solution yet.Trustful
what is the simple solution?Adjectival
well, divide the file into, smaller size that is acceptable by crypto.subtle.digest. get the digest and prepend it to the next one,... The problem; this will not be the same digest as the whole file. Since the SHA-256 adds some padding at the end. This will be the non-standard usage of the SHA-256 but will have same collusion resistance, pre-image and second pre-image attack resistance.Trustful
Your code works for any size of buffer. So, what doesn't work for large files?Dirichlet
#8974875Trustful
Oh, I see. Yes, it looks like that particular API is useless for hashing gigabytes of data.Dirichlet

© 2022 - 2024 — McMap. All rights reserved.