/* borrowed sha3 implementation from https://keccak.team */ #ifdef BUILD_PROD #include #endif #include #include #include #include "libcrypto.h" #include "keccak.h" /* md @name lua/kore ### sha3 Provides a sha3 implementation. Uses the header-only library from https://keccak.team Parameters: 0. data - {{ lua/string }} - The data to hash Returns: 0. data - {{ lua/string }} - The hash as a string. May contain embedded nulls. Example: local data = "Hello, world!" local hashed_data = sha3(data) print(hashed_data) */ int lsha3(lua_State *L){ size_t len; unsigned char out[64]; const unsigned char *data = (const unsigned char*)luaL_checklstring(L,-1,&len); lua_pop(L,1); FIPS202_SHA3_512(data, len, out); lua_pushlstring(L,(char*)out,64); return 1; } /* string xor sxor(a::string, b::string)::string */ int lsxor(lua_State *L){ size_t la,lb; const char *a = luaL_checklstring(L,-1,&la); const char *b = luaL_checklstring(L,-2,&lb); size_t outsize = la > lb ? la : lb; size_t loopsize = la > lb ? lb : la; const char *shorter = la > lb ? b : a; const char *longer = la > lb ? a : b; char out[outsize]; size_t i; for(i = 0; i < loopsize; i++) out[i] = shorter[i] ^ longer[i]; for(;i < outsize; i++) out[i] = longer[i]; lua_pushlstring(L,out,outsize); return 1; } static const luaL_Reg crypto_funcs[] = { {"sha3", lsha3}, {"sxor", lsxor}, {NULL,NULL} }; void load_crypto_libs(lua_State *L){ lua_getglobal(L,"_G"); luaL_register(L,NULL,crypto_funcs); lua_pop(L,1); }