Module crypto not found

Posted on
  • I'm trying to implement aes in my project by reading the test_crypto.js file. However, I am getting Module crypto not found. I'm using the 1v84 firmware. Is there a custom firmware that I should be using instead?

    code:

    ArrayBuffer.prototype.toHex = function () {
      var s = "";
      for (var i=0;i<this.length;i++)
        s += (256+this[i]).toString(16).substr(-2);
      return s;
    };
    
    ArrayBuffer.prototype.toStr = function () {
      return E.toString(this);
    };
    
    function fromHex(hex) {
      var arr = new ArrayBuffer(hex.length/2);
      for (var i=0;i<hex.length;i+=2)
        arr[i>>1] = parseInt(hex.substr(i,2),16);
      return arr;
    }
    
    function test(a, b) {
      tests++;
      var ar = a();
      if (ar!=b) {
        console.log("Test "+tests,a,"did not equal",b,", it was ", ar);
      } else {
       testPass++;
      }
    }
    
    var tests = 0;
    var testPass = 0;
    test(function() {
      return require('crypto').PBKDF2('Secret Passphrase', fromHex("cbde29d15836ce94e34a124afe1094e­2")).toHex();
    }, "dd469421e5f4089a1418ea24ba37c61b");
    
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = 'Lots and lots of my lovely secret data          ';
      return require('crypto').AES.encrypt(msg, key).toHex();
    }, "b7e812f9a05778e9fb2a09b9edf49e1f12e7543­e609fa8bec3d9ab82b5b206f59ff23b5614175f5­9d66918a1f271e5eb");
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = fromHex("b7e812f9a05778e9fb2a09b9edf49e1­f12e7543e609fa8bec3d9ab82b5b206f59ff23b5­614175f59d66918a1f271e5eb");
      return require('crypto').AES.decrypt(msg, key).toStr();
    }, 'Lots and lots of my lovely secret data          ');
    
    var iv = "Hello World 1234"; // 16 bytes (can be an array too)
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = 'Lots and lots of my lovely secret data          ';
      return require('crypto').AES.encrypt(msg, key, {iv:iv}).toHex();
    }, "66a140b8d735597643d4dfeb1f5b8f23516363e­9f7760d6a5bbc8659f0a9bccf7fdd55dfc1fc849­45443fdfe877238ed");
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = fromHex("66a140b8d735597643d4dfeb1f5b8f2­3516363e9f7760d6a5bbc8659f0a9bccf7fdd55d­fc1fc84945443fdfe877238ed");
      return require('crypto').AES.decrypt(msg, key, {iv:iv}).toStr();
    }, 'Lots and lots of my lovely secret data          ');
    
  • I wasn't sure if that code would work as is so I commented out all that code and just wrote:

    require('crypto');
    

    I'm still receiving

    Module crypto not found

  • As an orange warning? If so, it may still work - it would just be that the web IDE doesn't understand that the board contains the library.

  • @Gordon yes, the error is an orange warning.

  • So does the code you uploaded still work? You should be able to just ignore that warning.

  • @Gordon below is the code that I am now demoing out. The console says Done however, I don't see(know of) any of the test functions execute.

    ArrayBuffer.prototype.toHex = function () {
      var s = "";
      for (var i=0;i<this.length;i++)
        s += (256+this[i]).toString(16).substr(-2);
      return s;
    };
    
    ArrayBuffer.prototype.toStr = function () {
      return E.toString(this);
    };
    
    function fromHex(hex) {
      var arr = new ArrayBuffer(hex.length/2);
      for (var i=0;i<hex.length;i+=2)
        arr[i>>1] = parseInt(hex.substr(i,2),16);
      return arr;
    }
    
    function test(a, b) {
      tests++;
      var ar = a();
      if (ar!=b) {
        console.log("Test "+tests,a,"did not equal",b,", it was ", ar);
      } else {
       testPass++;
      }
    }
    
    var tests = 0;
    var testPass = 0;
    test(function() {
      return require('crypto').PBKDF2('Secret Passphrase', fromHex("cbde29d15836ce94e34a124afe1094e­2")).toHex();
    }, "dd469421e5f4089a1418ea24ba37c61b");
    
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = 'Lots and lots of my lovely secret data          ';
      return require('crypto').AES.encrypt(msg, key).toHex();
    }, "b7e812f9a05778e9fb2a09b9edf49e1f12e7543­e609fa8bec3d9ab82b5b206f59ff23b5614175f5­9d66918a1f271e5eb");
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = fromHex("b7e812f9a05778e9fb2a09b9edf49e1­f12e7543e609fa8bec3d9ab82b5b206f59ff23b5­614175f59d66918a1f271e5eb");
      return require('crypto').AES.decrypt(msg, key).toStr();
    }, 'Lots and lots of my lovely secret data          ');
    
    var iv = "Hello World 1234"; // 16 bytes (can be an array too)
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = 'Lots and lots of my lovely secret data          ';
      return require('crypto').AES.encrypt(msg, key, {iv:iv}).toHex();
    }, "66a140b8d735597643d4dfeb1f5b8f23516363e­9f7760d6a5bbc8659f0a9bccf7fdd55dfc1fc849­45443fdfe877238ed");
    
    test(function () {
      var key = fromHex("dd469421e5f4089a1418ea24ba37c61­b");
      var msg = fromHex("66a140b8d735597643d4dfeb1f5b8f2­3516363e9f7760d6a5bbc8659f0a9bccf7fdd55d­fc1fc84945443fdfe877238ed");
      return require('crypto').AES.decrypt(msg, key, {iv:iv}).toStr();
    }, 'Lots and lots of my lovely secret data          ');
    
    console.log("Done");
    
  • I'm not really sure what you're expecting - the code doesn't print anything unless there's an error. If you added some print statements to the code then you'd see something. Try changing test to:

    function test(a, b) {
      tests++;
      var ar = a();
      console.log("Expecting "+b);
      console.log("Got "+ar);
      if (ar!=b) {
        console.log("Test "+tests,a,"did not equal",b,", it was ", ar);
      } else {
       testPass++;
      }
    }
    

    Or even just copy/paste some of the commands out and try running them on Espruino's console.

  • ... or look at testPass and tests variables and see if they're nonzero.

  • @Gordon That is exactly what I was expecting... Each test function output something so I can see the crypto in action. I was able to see all of the test functions execute.

    OUTPUT:

    Expecting dd469421e5f4089a1418ea24ba37c61b Got
    dd469421e5f4089a1418ea24ba37c61b Expecting
    b7e812f9a05778e9fb2a09b9edf49e1f12e7543e­609fa8bec3d9ab82b5b206f59ff23b5614175f59­d66918a1f271e5eb
    Got
    b7e812f9a05778e9fb2a09b9edf49e1f12e7543e­609fa8bec3d9ab82b5b206f59ff23b5614175f59­d66918a1f271e5eb
    Expecting Lots and lots of my lovely secret data Got Lots and lots of
    my lovely secret data Expecting
    66a140b8d735597643d4dfeb1f5b8f23516363e9­f7760d6a5bbc8659f0a9bccf7fdd55dfc1fc8494­5443fdfe877238ed
    Got
    66a140b8d735597643d4dfeb1f5b8f23516363e9­f7760d6a5bbc8659f0a9bccf7fdd55dfc1fc8494­5443fdfe877238ed
    Expecting Lots and lots of my lovely secret data Got Lots and lots of
    my lovely secret data Done

    Now to utilize the crypto module for my real life project :)

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

Module crypto not found

Posted by Avatar for d0773d @d0773d

Actions