FFI binding of Argon2 for LuaJIT.
While lua-argon2 provides a PUC Lua binding through the Lua C API, this module is a binding for the LuaJIT FFI, especially fit for use in ngx_lua/OpenResty.
The Argon2 shared library must be compiled and available in your system.
Compatibility:
- Version
0.x
of this module is compatible with Argon220151206
- Version
1.x
of this module is compatible with Argon220160406
and later.
See the CI builds for the status of the currently supported versions.
This binding can be installed via Luarocks:
$ luarocks install argon2-ffi
Or simply by copying the src/argon2.lua
file in your LUA_PATH
.
Note: lua-argon2-ffi uses the same API as lua-argon2, to the exception of the default settings capabilities of lua-argon2.
Encrypt:
local argon2 = require "argon2"
--- Prototype
-- local hash, err = argon2.encrypt(pwd, salt, opts)
--- Argon2i
local hash = assert(argon2.encrypt("password", "somesalt"))
-- hash is "$argon2i$m=12,t=2,p=1$c29tZXNhbHQ$ltrjNRFqTXmsHj++TFGZxg+zSg8hSrrSJiViCRns1HM"
--- Argon2d
local hash = assert(argon2.encrypt("password", "somesalt", {argon2d = true}))
-- hash is "$argon2d$m=12,t=2,p=1$c29tZXNhbHQ$mfklun4fYCbv2Hw0UnZZ56xAqWbjD+XRMSN9h6SfLe4"
-- Hashing options
local hash = assert(argon2.encrypt("password", "somesalt", {
t_cost = 4,
m_cost = 24,
parallelism = 2,
hash_len = 64
}))
-- hash is "$argon2i$v=19$m=24,t=4,p=2$c29tZXNhbHQ$NV3zeCzIhUhosd7jtTuifTEoPOb/aPAtO0oTYdZkWfNBXCglBgxVEiJy+tLG4j011vZRO3pnmG82Vc/C1B6Tzw"
Verify:
local argon2 = require "argon2"
--- Prototype
-- local ok, err = argon2.decrypt(hash, plain)
local hash = assert(argon2.encrypt("password", "somesalt"))
-- hash is an argon2i hash
assert(argon2.verify(hash, "password")) -- ok: true
assert(argon2.verify(hash, "passworld")) -- error: The password did not match
This module's API being the same as lua-argon2's, the detailed documentation is available at http://thibaultcha.github.io/lua-argon2.
Work licensed under the MIT License. Please check P-H-C/phc-winner-argon2 for license over Argon2 and the reference implementation.