get SHA1 hash

Posted on
Page
of 2
Prev
/ 2
  • @d0773d I'm not sure that would really help... The majority of the work would just be getting WebSockets to work with a JS API - as all the header parsing and connection stuff is already part of Espruino.

    As I posted right at the top or this thread, there's a bug open for support where I pasted the code that basically sets up the server side of this. All it needs is the SHA1 hash - which is part of mbedtls in the AES branch on GitHub. The whole thing can also be developed and tested under Linux.

    It shouldn't be a lot of work, what it needs is for someone to write some C code - but it looks like that person might end up being me.

  • I've managed to get web sockets working using @JumJum 's js implementation from here:
    http://forum.espruino.com/comments/12723­204/

    It would be great to get SHA1 function into the core.

    I've attempted to build the crypto module for the esp8266, however get build errors (sorry can't recall what they were specifically)

    I wondering if the hashlib library would be a good place to add the sha1 function, so then the ws.js could use this rather than rely on crypto?

  • Which board are you doing this on? The Pico would 'just work'.

    It would be great to get SHA1 function into the core.

    It is in the core though. You just need the crypto library to be built in.

    I wondering if the hashlib library would be a good place to add the sha1 function

    Seriously, just figure out why mbedtls library isn't building (I guess this is for ESP8266?) rather than just duplicating something that already exists and works fine.

    I'm planning to remove hashlib at some point. Crypto is trying to be similar to CryptoJS so should be easy for people to get to grips with. mbedtls is great, and will be an easy source of loads more crypto functions if/when people want them as well.

    Perhaps one issue is that you only want SHA1 and not AES/etc - but that could be tweaked with some ifdefs pretty easily.

  • I'm trying to build crypto for esp8266.

    CC libs/crypto/jswrap_crypto.o
    libs/crypto/jswrap_crypto.c: In function 'jswrap_crypto_error':
    libs/crypto/jswrap_crypto.c:91:3: error: invalid initializer
       if (e) jsError(e);
       ^
    libs/crypto/jswrap_crypto.c: At top level:JsVar
    
    *jswrap_crypto_error_to_jsvar(int err) {
      const char *e = jswrap_crypto_error_to_str(err);
      if (e) return jsvNewFromString(e);
      return jsvVarPrintf("-0x%x", -err);
    }
    
    void jswrap_crypto_error(int err) {
      const char *e = jswrap_crypto_error_to_str(err);
    >>>  if (e) jsError(e);
      else jsError("Unknown error: -0x%x", -err);
    }
    

    The issue appears to be the flash_str #define:

    [#define](https://forum.espruino.com/sea­rch/?q=%23define) jsError(fmt, ...) do { \
        FLASH_STR(flash_str, fmt); \
        jsError_flash(flash_str, ##__VA_ARGS__); \
      } while(0)
    
    root@esp8266-VirtualBox:~/Espruino# grep '#define FLASH_STR' src/*.h
    src/jsutils.h:#define FLASH_STR(name, x) static char name[] __attribute__((section(".irom.literal"))­) __attribute__((aligned(4))) = x
    

    static char name[] vs const char *

    Is there a simple way to resolve this?

    I'm not sure the protocol here - should I raise as a github issue?

  • It looks like you have? it's probably best to discuss build-related stuff in there...

  • Yes - I did raise it on github -I thought I would document what I had discovered while it was fresh.

  • I've written a crypto module to use on boards without the crypto module in firmware - it implements the SHA1 used by the web sockets module. The ws module does not need to be modified.

    For testing if the crypto.js file is copied down to a web ide project it can be tested.

    The following code assumes the ESP8266 has a wifi.save and is connected to a network.

    var page = '<!DOCTYPE html><html lang=en><head><meta charset=utf-8>';
    page += ' <meta http-equiv=X-UA-Compatible content="IE=edge">';
    page += '<meta name=viewport content="width=device-width,initial-scal­e=1">';
    page += '<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/boo­tstrap/3.3.5/css/bootstrap.min.css">';
    page += '</head><body>';
    page += '<div class="well well-lgt">';
    page += '<div class="panel-heading"><h3>Websockets</h3­></div>';
    page += '<button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;th­is.innerHTML=\'Send \'+count">Send to Espruino</button>';
    page += '<ul id="list" class="list-group"></ul>';
    page += '</div>';
    page += '<script>';
    page += 'var ws,count=1;setTimeout(function(){';
    page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
    page += 'ws.onmessage = function (event) { console.log({msg:event.data});';
    page += 'var ul = document.getElementById("list");';
    page += 'var li = document.createElement("li");';
    page += 'li.className="list-group-item";';
    page += 'li.appendChild(document.createTextNode(­event.data));';
    page += 'ul.appendChild(li);';
    page += '};';
    page += 'setTimeout(function() { ws.send("Hello to Espruino!");ws.send(JSON.stringify({brow­ser:95})); }, 1000);';
    page += '},1000);</script></body></html>';
    function onPageRequest(req, res) {
      res.writeHead(200, {
        'Content-Type' : 'text/html'
      });
      res.end(page);
    }
    var server = require('ws').createServer(onPageRequest­);
    server.listen(80);
    server.on("websocket", function (ws) {
      ws.on('message', function (msg) {
        console.log({
          ws : msg
        });
        if (msg % 2 === 0)
          ws.send(msg + ' is multiple of two');
      });
      ws.send("Hello from Espruino!");
      ws.send(JSON.stringify({
          date : Date.now()
        }));
    
    });
    

    Start a browser at your Eps8266 address and see:

    If you click the button, you can see the event appear in the Web IDE console. Every 2nd click causes the server to send back to the browser.... two way socket!

    https://github.com/wilberforce/EspruinoD­ocs/blob/master/modules/crypto.js

    For clarity, this the code that gets sent to the browser, using a CDN to get the bootstrap css:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scal­e=1"><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/boo­tstrap/3.3.5/css/bootstrap.min.css">
    </head>
       <body>
       	<div class="well well-lgt">
       		<div class="panel-heading"><h3>Websockets</h3­>
       		</div>
       	    <button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;th­is.innerHTML='Send '+count">
    Send to Espruino</button>
       		<ul id="list" class="list-group">
       		</ul>
       	</div>
    <script>
    var ws, count = 1;
    setTimeout(function () {
      ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");
      ws.onmessage = function (event) {
        console.log({
          msg : event.data
        });
        var ul = document.getElementById("list");
        var li = document.createElement("li");
        li.className = "list-group-item";
        li.appendChild(document.createTextNode(e­vent.data));
        ul.appendChild(li);
      };
      setTimeout(function () {
        ws.send("Hello to Espruino!");
        ws.send(JSON.stringify({
            browser : 95
          }));
      }, 1000);
    </script>
       </body>
    </html>
    

    2 Attachments

  • Nice - thanks for posting it up!

    We just need to find a nice way of adding it such that it doesn't interfere with the in-built crypto library on other boards.

  • Sure.

    I was hoping that a require("crypto") would look for a native install first, and then look in modules on espruino site.

    Does it not work this way?

    The other thing I tried and could quite pull off was using a require("path to github raw") and then attempted to add that to the modules cache, so that a later require("crypto") would find the module already loaded.

  • I was hoping that a require("crypto") would look for a native install first, and then look in modules on espruino site.

    Yes - it should do this, but it needs a bit of tweaking on my side first as currently the list of internal modules reported by espruino.com isn't entirely accurate.

    and then attempted to add that to the modules cache

    You could probably do something really hacky by modifying the module cache directly:

    global["\xFF"].modules["crypto"] =  global["\xFF"].modules["http://..."];
    
  • Works great! Thanks!

  • I've managed to get the sha functions compiling on the esp8266 build, by disabling the aes functions. I'll get that up on github and do a pull request. Then this can be part of the esp8266 core, with little additional overhead on the current build...

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

get SHA1 hash

Posted by Avatar for JumJum @JumJum

Actions