与这个问题相关的代码在这里:https ://github.com/jchester/lua-polarssl/tree/master/src
目前我正在尝试包装 PolarSSL 库 (http://polarssl.org) 的一部分,以便让我访问 SHA-512 HMAC(luacrypto 不提供此功能)。
我瞄准的 API 是这样的形式:
a_sha512_hash = polarssl.hash.sha512('text')
或更充分
local polarssl = require 'polarssl'
local hash = polarssl.hash
a_sha512_hash = hash.sha512('test')
如果您在上面的链接中引用 polarssl.c,您会看到我编写了包装 PolarSSL 代码的函数。然后我正在尝试构建函数表:
LUA_API int luaopen_polarssl( lua_State *L ) {
static const struct luaL_Reg core[] = {
{ NULL, NULL }
};
static const struct luaL_Reg hash_functions[] = {
{ "sha512", hash_sha512 },
{ "sha384", hash_sha384 },
{ NULL, NULL }
};
static const struct luaL_Reg hmac_functions[] = {
{ "sha512", hmac_sha512 },
{ "sha384", hmac_sha384 },
{ NULL, NULL }
};
luaL_register( L, CORE_MOD_NAME, core );
luaL_register( L, HASH_MOD_NAME, hash_functions );
luaL_register( L, HMAC_MOD_NAME, hmac_functions );
return 1;
}
其中 CORE_MOD_NAME = 'polarssl',HASH_MOD_NAME = 'polarssl.hash',HMAC_MOD_NAME = 'polarssl.hmac'。
当我在这个问题的顶部运行类似于 Lua 代码的测试脚本时,我得到了这个:
lua: test.lua:23: attempt to index global 'polarssl' (a nil value)
stack traceback:
test.lua:23: in main chunk
[C]: ?
我尝试寻找如何实现这种 module.submodule 方法的示例(例如naim vs luasockets),但每个人似乎都有不同的实现方式。我完全迷路了。