title: CryptoJS Maximum Call Stack Size Exceeded in wordArray to ByteArray conversion
body: <p>I am trying to implement encryption and decryption in a react client application using CryptoJS. The encryption and decryption can be done without error but it is only for the case of small file size. Errors occurs saying that <strong>Maximum call stack size exceeded</strong> in converting wordArray to ByteArray during decryption of a video file(9MB). The code handling this conversion is from: <a href="https://i.stack.imgur.com/5Prhg.png" rel="nofollow noreferrer">GitHub</a></p>
  
  <p>Error: 
  <a href="https://i.stack.imgur.com/5Prhg.png" rel="nofollow noreferrer">Maximum call stack</a><a href="https://i.stack.imgur.com/ao6uQ.png" rel="nofollow noreferrer"> size exceeded</a></p>
  
  <p>Code:</p>
  
  <pre><code> wordArrayToByteArray = (wordArray,length) =&gt; {
  
    if(wordArray.hasOwnProperty("sigBytes") &amp;&amp; wordArray.hasOwnProperty("words")){
      length = wordArray.sigBytes;
      wordArray = wordArray.words;
    }
  
    let result = [];
    let bytes;
    let i = 0;
  
    while(length &gt; 0){
      bytes = this.wordToByteArray(wordArray[i],Math.min(4,length));
      length -= bytes.length;
      result.push(bytes);
      i++;
    }
  
    return [].concat.apply([],result);
  }
  
   wordToByteArray = (word, length) =&gt; {
  
    var ba = [],
      i,
      xFF = 0xFF;
    if (length &gt; 0)
      ba.push(word &gt;&gt;&gt; 24);
    if (length &gt; 1)
      ba.push((word &gt;&gt;&gt; 16) &amp; xFF);
    if (length &gt; 2)
      ba.push((word &gt;&gt;&gt; 8) &amp; xFF);
    if (length &gt; 3)
      ba.push(word &amp; xFF);
  
    return ba;
  }
  
  decryptFile = (downloaded) =&gt;{ 
    //convert unit array(encrypted file) to string
    let encryptedString = this.uintToString(downloaded);
  
    //decrypt and get wordArray
    const decrypted = cryptojs.AES.decrypt(encryptedString,"123");
    console.log(decrypted);
  
    //convert wordArray to string
    let decryptedString = decrypted.toString(cryptojs.enc.Utf8);
  
  
    // form a new word array
    const wordArray = cryptojs.enc.Hex.parse(decryptedString);
  
  
    //convert new wordArray to byteArray
    return this.wordArrayToByteArray(wordArray,Object.keys(wordArray).length);
  }
  
  uintToString = (uintArray) =&gt; {
    const decodedStr = new TextDecoder("utf-8").decode(uintArray);
    return decodedStr;
  }
  
   onDownload = () =&gt; {
    ipfs.get(this.state.ipfsHash,(error,files)=&gt;{
      files.forEach((file)=&gt;{
        console.log(file.path);
        const decryptedFile = this.decryptFile(file.content);
  
        const arrayBufferView = new Uint8Array(decryptedFile);
  
        const blob = new Blob([arrayBufferView],{type: "video/mp4"});
        console.log(blob);
        fileSaver.saveAs(blob,"curseTheInternet.mp4");
      })
    })
  }
  </code></pre>
  
  <p>Or any other suggestion of encryption library for client?   </p>