Module "crypto" not found when using was module

Posted on
  • Whenever I require("ws") it gives an error saying module crypto not found. I have tried loading the crypto module from node into a SD card under node_modules, but it still doesn't work.

  • What board are you trying to run this on, and is the firmware up to date?

  • Its the original espruino board, with firmware v96 and v95 not working. Haven't tried older versions yet.

  • Ahh. Unfortunately I don't think the Original Espruino Board ever had enough free flash memory to be able to include the crypto module by default (it's not JS, it's a built-in C module)

    @Wilberforce made a JS version with just the function that's needed for websockets though:­276175/?offset=25#12817146

    If you add the following at the top of your code:

    Modules.addCached("crypto",function() {
      function f(s, x, y, z)  {
        if(s===0) return (x & y) ^ (~x & z);
        else if(s===1) return  x ^ y  ^  z; 
        else if(s===2) return (x & y) ^ (x & z) ^ (y & z); 
        else return x ^ y  ^  z;
      function split(n){ 
        for(i=3; i>=0;i--){
          M.push((n>>(i*8)) & 0xff);
      var K,N,M,i,j,H0,H1,H2,H3,H4;
      var W,a,b,c,d,e,s,T;
      var flt = 0xffffffff; 
      // I'm not sure why this is needed?
      msg += String.fromCharCode(0x80);
      K = new Int32Array([ 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 ]);
      N = Math.ceil((msg.length/4 + 2)/16);
      M = new Array(N);
      msg = E.toUint8Array(msg);
      for (i=0; i<N; i++) {
        a = i <<6;
        c = new Int32Array(16);
          b = a +(j<<2);
          c[j] = (msg[b]<<24) | (msg[b+1]<<16) | (msg[b+2]<<8) | (msg[b+3]);
        M[i] = c;
      M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]);
      M[N-1][15] = ((msg.length-1)*8) & flt;
      H0 = 0x67452301;H1 = 0xefcdab89;H2 = 0x98badcfe;H3 = 0x10325476;H4 = 0xc3d2e1f0;
      W = new Int32Array(80);
      for (i=0; i<N; i++) {
        for (j=0;  j<16; j++) W[j] = M[i][j];
        for (j=16; j<80; j++){
          a = W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16];
          W[j] = (a<<1)|(a>>>31);
        a = H0; b = H1; c = H2; d = H3; e = H4;
        for (j=0; j<80; j++) {
          s = Math.floor(j/20); 
          T = (((a<<5) | (a>>>27)) + f(s,b,c,d) + e + K[s] + W[j]) & flt;
          e = d; d = c; c = (b<<30) | (b>>>2);b = a;a = T;
        H0 = (H0 + a) & flt;H1 = (H1 + b) & flt; H2 = (H2 + c) & flt;
        H3 = (H3 + d) & flt;H4 = (H4 + e) & flt;
      M = [];
      return M;

    Then that should fix it for you.

  • @Gordon
    The esp8266 build only includes the Sha functions - and not the complete library - so that is smaller, so there a possibility that might fit?

  • It looks like it's still a bit tight on the Espruino board. It would just fit though, but probably not enough for the next release.

    Looking at it, the ESP8266 version still includes SHA224/256/384/512 as well - so realistically I could drop those and it'd leave enough room. I'll try and do that in the next few weeks.

  • I see hashlib is still there - does that take much space?­b/master/boards/

    I suppose people might be using that too!

  • @Gordon thanks so much!!! I really needed this. I will try it later today and see if it works.

  • It worked, but not with the modules.addCached. I had to modify the websockets module to just use

    var crypto = {SHA1: (Sha function here)};

    Instead of

    var crypto = require('crypto');

    Thanks a lot!

  • Thanks for the update! Strange about the module though - it seems to work fine here. Could you have been trying it with an older firmware version?

  • No, I was using the latest version. I might have messed something else up though. Thanks for the help!

  • Ahh, sorry - I think the issue is the WebSockets library was requesting the crypto library at load time rather than when it was needed. I'll tweak it so that next time I do an update that'll be sorted - but at least you have a modified version working!

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

Module "crypto" not found when using was module

Posted by Avatar for user88503 @user88503