如何使用luacrypto?

时间:2017-06-14 15:57:40

标签: lua openssl

我正在尝试将mkottman / luacrypto与Xcode一起使用。我将lcrypto.c添加到我的项目中并进行编译。这是成功的。现在我用简单的代码创建了简单的.lua文件:

android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"

当我跑步时,我收到错误crypto = require 'crypto' assert(crypto.hex, "missing crypto.hex")

有人可以帮忙吗?我错过了什么?

2 个答案:

答案 0 :(得分:1)

Lua无法找到该库。您应该会在搜索文件的位置看到错误。例如:

[string "REPL"]:1: module 'crypto' not found:No LuaRocks module found for crypto
        no field package.preload['crypto']
        no file '/home/phil/.luarocks/share/lua/5.1/crypto.lua'
        no file '/home/phil/.luarocks/share/lua/5.1/crypto/init.lua'
        no file '/usr/local/share/lua/5.1/crypto.lua'
        no file '/usr/local/share/lua/5.1/crypto/init.lua'
        no file './crypto.lua'
        no file '/usr/local/lib/lua/5.1/crypto.lua'
        no file '/usr/local/lib/lua/5.1/crypto/init.lua'
        no file '/usr/share/lua/5.1/crypto.lua'
        no file '/usr/share/lua/5.1/crypto/init.lua'
        no file '/home/phil/.luarocks/lib/lua/5.1/crypto.so'
        no file '/usr/local/lib/lua/5.1/crypto.so'
        no file './crypto.so'
        no file '/usr/lib/x86_64-linux-gnu/lua/5.1/crypto.so'
        no file '/usr/lib/lua/5.1/crypto.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        /usr/local/share/lua/5.1/repl/init.lua:73: in function </usr/local/share/lua/5.1/repl/init.lua:72>
        (tail call): ?
        [C]: in function 'require'
        [string "REPL"]:1: in main chunk
        [C]: in function 'xpcall'
        /usr/local/share/lua/5.1/repl/init.lua:101: in function 'old_value'
        /usr/local/share/lua/5.1/repl/init.lua:250: in function 'handleline'
        /usr/local/share/lua/5.1/repl/sync.lua:33: in function 'run'
        ...r/local/lib/luarocks/rocks/luarepl/0.8-1/bin/rep.lua:41: in main chunk
        [C]: ?

作为替代方案,您可以通过包管理器luarocks安装它:

$ luarocks search luacrypto

Search results:
===============


Rockspecs and source rocks:
---------------------------

luacrypto
   0.3.2-2 (rockspec) - https://luarocks.org
   0.3.2-2 (src) - https://luarocks.org
   0.3.2-1 (rockspec) - https://luarocks.org
   0.3.2-1 (src) - https://luarocks.org
   0.3.0.20120524-1 (rockspec) - https://luarocks.org
   0.3.0.20120524-1 (src) - https://luarocks.org
   0.2.0-2 (rockspec) - https://luarocks.org
   0.2.0-2 (src) - https://luarocks.org
   0.2.0-1 (rockspec) - https://luarocks.org
   0.2.0-1 (src) - https://luarocks.org

luacryptor
   1.0-1 (rockspec) - https://luarocks.org

并使用luarocks install luacrypto安装。

答案 1 :(得分:0)

菲利普对于缺少模块是正确的。如果我将crypto.so添加到搜索路径中,则lua代码require "crypto"可以正常工作。 但我需要将luacrypto静态链接到我的c ++项目中。 我找到了以下解决方案。

//file.cpp:
luaopen_crypto(L);
lua_setglobal(L, "crypto");

-- file.lua:
--crypto = require "crypto"
print(crypto.hex("123"))

这只是一种解决方法。我将crypto设置为全局变量,我可以在.lua文件中使用它。但任何试图调用local crypto = require "crypto"都会失败。