How to read a binary file with FileReader in order to hash it with SHA-256 in CryptoJS?

后端 未结 1 1423
無奈伤痛
無奈伤痛 2020-12-06 19:01

how do I convert a UTF-8 string to Latin1 encoded string using javascript?

Here is what I am trying to do:

  1. I get a file, split that in chunks by readin
1条回答
  •  时光取名叫无心
    2020-12-06 19:52

    CryptoJS doesn't understand what an ArrayBuffer is and if you use some text encoding like Latin1 or UTF-8, you will inevitably lose some bytes. Not every possible byte value has a valid encoding in one of those text encodings.

    You will have to convert the ArrayBuffer to CryptoJS' internal WordArray which holds the bytes as an array of words (32 bit integers). We can view the ArrayBuffer as an array of unsigned 8 bit integers and put them together to build the WordArray (see arrayBufferToWordArray).

    The following code shows a full example:

    function arrayBufferToWordArray(ab) {
      var i8a = new Uint8Array(ab);
      var a = [];
      for (var i = 0; i < i8a.length; i += 4) {
        a.push(i8a[i] << 24 | i8a[i + 1] << 16 | i8a[i + 2] << 8 | i8a[i + 3]);
      }
      return CryptoJS.lib.WordArray.create(a, i8a.length);
    }
    
    function handleFileSelect(evt) {
      var files = evt.target.files; // FileList object
    
      // Loop through the FileList and render image files as thumbnails.
      for (var i = 0, f; f = files[i]; i++) {
        var reader = new FileReader();
    
        // Closure to capture the file information.
        reader.onloadend = (function(theFile) {
          return function(e) {
            var arrayBuffer = e.target.result;
    
            var hash = CryptoJS.SHA256(arrayBufferToWordArray(arrayBuffer));
            var elem = document.getElementById("hashValue");
            elem.value = hash;
          };
    
        })(f);
        reader.onerror = function(e) {
          console.error(e);
        };
    
        // Read in the image file as a data URL.
        reader.readAsArrayBuffer(f);
      }
    }
    
    document.getElementById('upload').addEventListener('change', handleFileSelect, false);
    
    
    Select image to upload:

    You can extend this code with the techniques in my other answer in order to hash files of arbitrary size without freezing the browser.

    0 讨论(0)
提交回复
热议问题