Skip to content

Commit

Permalink
Merge pull request #6 from thibaultCha/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
thibaultcha committed Apr 10, 2016
2 parents ae599a8 + c69ea67 commit b13dbb7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
1 change: 1 addition & 0 deletions .ci/setup_argon2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pushd $ARGON2_DIR
git checkout $ARGON2_VERSION
make
make test
ln -s libargon2.so libargon2.so.0
popd

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ notifications:
email: false
env:
global:
- ARGON2_VERSION=20151206
- ARGON2_VERSION=20160406
- ARGON2_DIR=$HOME/argon2
matrix:
- LUA="luajit 2.0"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The [Argon2] shared library must be compiled and available in your system.
Compatibility:
- Version `0.x` of this module is compatible with Argon2
[`20151206`](https://github.com/P-H-C/phc-winner-argon2/releases/tag/20151206)
- Version `1.x` of this module is compatible with Argon2
[`20160406`](https://github.com/P-H-C/phc-winner-argon2/releases/tag/20160406)

### Install

Expand Down Expand Up @@ -90,6 +92,6 @@ implementation.

[badge-travis-url]: https://travis-ci.org/thibaultCha/lua-argon2-ffi
[badge-travis-image]: https://travis-ci.org/thibaultCha/lua-argon2-ffi.svg?branch=master
[badge-version-image]: https://img.shields.io/badge/version-0.0.1-blue.svg?style=flat
[badge-version-image]: https://img.shields.io/badge/version-1.0.0-blue.svg?style=flat
[badge-coveralls-url]: https://coveralls.io/github/thibaultCha/lua-argon2-ffi?branch=master
[badge-coveralls-image]: https://coveralls.io/repos/github/thibaultCha/lua-argon2-ffi/badge.svg?branch=master
4 changes: 2 additions & 2 deletions argon2-ffi-0.0.1-0.rockspec → argon2-ffi-1.0.0-0.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package = "argon2-ffi"
version = "0.0.1-0"
version = "1.0.0-0"
source = {
url = "git://github.com/thibaultCha/lua-argon2-ffi",
tag = "0.0.1"
tag = "1.0.0"
}
description = {
summary = "LuaJIT FFI binding for the Argon2 password hashing algorithm",
Expand Down
23 changes: 10 additions & 13 deletions spec/argon2_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local argon2 = require "argon2"

describe("argon2_ffi", function()
describe("argon2", function()
it("_VERSION field", function()
assert.equal("0.0.1", argon2._VERSION)
assert.equal("1.0.0", argon2._VERSION)
end)
it("_AUTHOR field", function()
assert.equal("Thibault Charbonnier", argon2._AUTHOR)
Expand Down Expand Up @@ -41,17 +41,6 @@ describe("encrypt()", function()
argon2.encrypt("", "", {parallelism = ""})
end, "expected parallelism to be a number")
end)
it("should return a hash", function()
local hash = assert(argon2.encrypt("password", "somesalt"))
assert.equal("$argon2i$m=12,t=2,p=1$c29tZXNhbHQ$ltrjNRFqTXmsHj++TFGZxg+zSg8hSrrSJiViCRns1HM", hash)

hash = assert(argon2.encrypt("password2", "somesalt2"))
assert.equal("$argon2i$m=12,t=2,p=1$c29tZXNhbHQy$nb+IxJGGftnQu8G8CMMOlqIOmz+0SK9/8/1fl5m/7GQ", hash)
end)
it("should hash with argon2d", function()
local hash = assert(argon2.encrypt("password", "somesalt", {argon2d = true}))
assert.matches("argon2d", hash)
end)
it("salt too short", function()
local hash, err = argon2.encrypt("password", "")
assert.falsy(hash)
Expand All @@ -61,6 +50,14 @@ describe("encrypt()", function()
assert.falsy(hash)
assert.equal("Salt is too short", err)
end)
it("should return a hash", function()
local hash = assert(argon2.encrypt("password", "somesalt"))
assert.matches("$argon2i$v=19$m=12,t=2,p=1$", hash, nil, true)
end)
it("should hash with argon2d", function()
local hash = assert(argon2.encrypt("password", "somesalt", {argon2d = true}))
assert.matches("argon2d", hash)
end)
it("should accept time cost", function()
local hash = assert(argon2.encrypt("password", "somesalt", {t_cost = 4}))
assert.matches("t=4", hash)
Expand Down
38 changes: 26 additions & 12 deletions src/argon2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ local OPTIONS = {
ffi.cdef [[
typedef enum Argon2_type { Argon2_d = 0, Argon2_i = 1 } argon2_type;

int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt, const size_t saltlen,
void *hash, const size_t hashlen, char *encoded,
const size_t encodedlen, argon2_type type);
int argon2i_hash_encoded(const uint32_t t_cost,
const uint32_t m_cost,
const uint32_t parallelism,
const void *pwd, const size_t pwdlen,
const void *salt, const size_t saltlen,
const size_t hashlen, char *encoded,
const size_t encodedlen);

int argon2d_hash_encoded(const uint32_t t_cost,
const uint32_t m_cost,
const uint32_t parallelism,
const void *pwd, const size_t pwdlen,
const void *salt, const size_t saltlen,
const size_t hashlen, char *encoded,
const size_t encodedlen);

int argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
argon2_type type);

const char *error_message(int error_code);
const char *argon2_error_message(int error_code);
]]

local buf = ffi_new("char[?]", ENCODED_LEN)
Expand All @@ -38,7 +48,7 @@ local c_type_d = ffi_new(argon2_t, "Argon2_d")
local lib = ffi.load "argon2"

local _M = {
_VERSION = "0.0.1",
_VERSION = "1.0.0",
_AUTHOR = "Thibault Charbonnier",
_LICENSE = "MIT",
_URL = "https://github.com/thibaultCha/lua-argon2-ffi",
Expand Down Expand Up @@ -66,15 +76,19 @@ function _M.encrypt(pwd, salt, opts)
end
end

local c_type = opts.argon2d and c_type_d or c_type_i
local res
if opts.argon2d then
res = lib.argon2d_hash_encoded(opts.t_cost, opts.m_cost, opts.parallelism,
pwd, #pwd, salt, #salt, HASH_LEN, buf, ENCODED_LEN)
else
res = lib.argon2i_hash_encoded(opts.t_cost, opts.m_cost, opts.parallelism,
pwd, #pwd, salt, #salt, HASH_LEN, buf, ENCODED_LEN)
end

local res = lib.argon2_hash(opts.t_cost, opts.m_cost, opts.parallelism,
pwd, #pwd, salt, #salt,
nil, HASH_LEN, buf, ENCODED_LEN, c_type)
if res == 0 then
return ffi_str(buf)
else
local c_msg = lib.error_message(res)
local c_msg = lib.argon2_error_message(res)
return nil, ffi_str(c_msg)
end
end
Expand Down

0 comments on commit b13dbb7

Please sign in to comment.