pipe js file to eval?

Posted on
  • I am trying to pipe my aes.js file to eval because the file is too large. My code:

    function onInit() {
      var fs = require('fs');
    
      var f = fs.readFile("aes.js");
      eval(f.pipe());
    }
    

    I get error:

    Uncaught Error: Field or method does not already exist, and can't
    create it on String at line 5 col 9 eval(f.pipe());

         ^ in function "onInit" called from line 1 col 8 onInit();
    
  • This may not solve your problem, but doesn't that crypto package you're playing with include minified versions of the files that are a lot smaller? That might make it possible to handle with without piping anything.

  • @DrAzzy using the minified version of the script didn't give me the out of memory error. However, I am still getting errors:

    Uncaught Error: Field or method does not already exist, and can't
    create it on undefined at line 1 col 24 {for(var
    q=CryptoJS,x=q.lib.BlockCipher,r=q.algo,­j=[],y=[],z...

            d           ^  at line 1 col 223 ...3^l<<4,f=f>>>8^f&255^99;j[n]=f;y[f]=n­;var t=g[n],D=g[t],E=g[...
                               ^ in function called from line 10 col 467 ...q.AES=x._createHelper(r)})();
                               ^ in function "onInit" called from line 1 col 8 onInit();
    

    I'm assuming the crypto package probably won't work on the Espruino.

  • That error was due to me not incorporating the core.js file. So I did:

    eval(require('fs').readFile("core-min.js­"));
      eval(require('fs').readFile("aes-min.js"­));
    

    I am now getting the out of memory error. For a test I commented out the aes file and just loaded the core-min.js. I didn't get the out of memory error when just loading the core-min.js file.

  • For the initial question - no, you can't pipe to eval I'm afraid. Espruino needs to have the whole string of code in memory to execute it.

    You may find the 'rollups' are a bit more efficient? For instance: http://crypto-js.googlecode.com/svn/tags­/3.0.2/build/rollups/aes.js

    However having looked at it, I think that AES implementation is too big to go in Espruino - not so much the code, but it allocates some arrays, and I think those fill up the available memory. With a bit of tweaking and changing to Int32Arrays you might be able to fit it in though.

    You could always use MD5, which seems to be a bit more efficient: http://crypto-js.googlecode.com/svn/tags­/3.0.2/build/rollups/md5.js

    You can actually copy the contents of the rollup, paste it into the Web IDE and do:

    function unescape(a){return a;} // this is needed by the MD5 lib...
    function encodeURIComponent(a){return a;} // this is needed by the MD5 lib...
    CryptoJS.MD5("Hello").toString()
    

    But having said that, the answer gained from the MD5 library on Espruino doesn't match what I get from a desktop JS implementation. I'll try and find out why, but I'm not sure when I'll have time, so if anyone is able to track down which operation in Espruino isn't working as expected it'd be hugely helpful!

  • I've just fixed a bug that stopped the MD5 implementation here from working on the Espruino board - that'll be in 1v68: http://pajhome.org.uk/crypt/md5/md5.html­

    the cryptojs MD5 still isn't working though...

  • @Gordon, Thank you for taking the time to assist me. Encryption by using the Espruino isn't a necessity. At the moment, I am looking for a wifi module that has hardware encryption. As far as the Lantronix xPico wifi module that I boasted about a few weeks ago, well, it turns out I was wrong about the built in encryption. I was reading the wrong product info LOL :/ It turns out the Lantronix xPico ethernet module supposedly supports AES encryption. So, now I am in the market to purchase another wifi module that supports AES encryption.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

pipe js file to eval?

Posted by Avatar for d0773d @d0773d

Actions