moonbridge

diff moonbridge_websocket.c @ 139:f148bd2b3d05

Added moonbridge_io.h to Makefile; Added stub for moonbridge_websocket.c (with base64'd SHA-1)
author jbe
date Sat Apr 18 01:33:38 2015 +0200 (2015-04-18)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/moonbridge_websocket.c	Sat Apr 18 01:33:38 2015 +0200
     1.3 @@ -0,0 +1,49 @@
     1.4 +
     1.5 +#include <lua.h>
     1.6 +#include <lauxlib.h>
     1.7 +#include <lualib.h>
     1.8 +
     1.9 +#include <sys/types.h>
    1.10 +#include <sha.h>
    1.11 +
    1.12 +static int moonbr_ws_hsresp(lua_State *L) {
    1.13 +  const char *challenge;
    1.14 +  size_t challenge_len;
    1.15 +  SHA_CTX context;
    1.16 +  char digest[20];
    1.17 +  static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    1.18 +  char digest64[28];
    1.19 +  int i, j;
    1.20 +  int s = 0, b = 0;
    1.21 +  challenge = luaL_checklstring(L, 1, &challenge_len);
    1.22 +  SHA1_Init(&context);
    1.23 +  SHA1_Update(&context, challenge, challenge_len);
    1.24 +  SHA1_Update(&context, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36);
    1.25 +  SHA1_Final((unsigned char *)digest, &context);
    1.26 +  for (i=0, j=0; i<20; i++) {
    1.27 +    b = (b << 8) + (unsigned char)digest[i];
    1.28 +    s += 8;
    1.29 +    while (s >= 6) {
    1.30 +      s -= 6;
    1.31 +      digest64[j++] = alphabet[b >> s];
    1.32 +      b &= (1<<s)-1;
    1.33 +    }
    1.34 +  }
    1.35 +  digest64[26] = alphabet[b << 2];
    1.36 +  digest64[27] = '=';
    1.37 +  lua_pushlstring(L, digest64, 28);
    1.38 +  return 1;
    1.39 +}
    1.40 +
    1.41 +static const struct luaL_Reg moonbr_ws_module_funcs[] = {
    1.42 +  {"hsresp", moonbr_ws_hsresp},
    1.43 +  {NULL, NULL}
    1.44 +};
    1.45 +
    1.46 +int luaopen_moonbridge_websocket(lua_State *L) {
    1.47 +  lua_newtable(L);  // module
    1.48 +  luaL_setfuncs(L, moonbr_ws_module_funcs, 0);
    1.49 +  return 1;
    1.50 +
    1.51 +}
    1.52 +

Impressum / About Us