moonbridge

view moonbridge_websocket.c @ 157:99a70d18e47c

Further work on new HTTP layer (code cleanup, work on body reading)
author jbe
date Wed May 27 01:51:04 2015 +0200 (2015-05-27)
parents f148bd2b3d05
children
line source
2 #include <lua.h>
3 #include <lauxlib.h>
4 #include <lualib.h>
6 #include <sys/types.h>
7 #include <sha.h>
9 static int moonbr_ws_hsresp(lua_State *L) {
10 const char *challenge;
11 size_t challenge_len;
12 SHA_CTX context;
13 char digest[20];
14 static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
15 char digest64[28];
16 int i, j;
17 int s = 0, b = 0;
18 challenge = luaL_checklstring(L, 1, &challenge_len);
19 SHA1_Init(&context);
20 SHA1_Update(&context, challenge, challenge_len);
21 SHA1_Update(&context, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36);
22 SHA1_Final((unsigned char *)digest, &context);
23 for (i=0, j=0; i<20; i++) {
24 b = (b << 8) + (unsigned char)digest[i];
25 s += 8;
26 while (s >= 6) {
27 s -= 6;
28 digest64[j++] = alphabet[b >> s];
29 b &= (1<<s)-1;
30 }
31 }
32 digest64[26] = alphabet[b << 2];
33 digest64[27] = '=';
34 lua_pushlstring(L, digest64, 28);
35 return 1;
36 }
38 static const struct luaL_Reg moonbr_ws_module_funcs[] = {
39 {"hsresp", moonbr_ws_hsresp},
40 {NULL, NULL}
41 };
43 int luaopen_moonbridge_websocket(lua_State *L) {
44 lua_newtable(L); // module
45 luaL_setfuncs(L, moonbr_ws_module_funcs, 0);
46 return 1;
48 }

Impressum / About Us