From fba79b3e25cca674aac783fa57428b23bb5b7135 Mon Sep 17 00:00:00 2001 From: huangpj Date: Wed, 8 Mar 2017 18:01:59 +0800 Subject: [PATCH 1/4] support pbkdf2 --- Makefile | 3 ++- src/emqttd_auth_mod.erl | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 913f11b89..558d66180 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT_VERSION = 2.1.0 NO_AUTOPATCH = cuttlefish -DEPS = gproc lager esockd mochiweb lager_syslog +DEPS = gproc lager esockd mochiweb lager_syslog pbkdf2 dep_gproc = git https://github.com/uwiger/gproc dep_getopt = git https://github.com/jcomellas/getopt v0.8.2 @@ -12,6 +12,7 @@ dep_lager = git https://github.com/basho/lager master dep_esockd = git https://github.com/emqtt/esockd master dep_mochiweb = git https://github.com/emqtt/mochiweb dep_lager_syslog = git https://github.com/basho/lager_syslog +dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0 ERLC_OPTS += +'{parse_transform, lager_transform}' diff --git a/src/emqttd_auth_mod.erl b/src/emqttd_auth_mod.erl index c27f49d36..4ddf4c067 100644 --- a/src/emqttd_auth_mod.erl +++ b/src/emqttd_auth_mod.erl @@ -22,7 +22,7 @@ -export([passwd_hash/2]). --type(hash_type() :: plain | md5 | sha | sha256). +-type(hash_type() :: plain | md5 | sha | sha256 | pbkdf2). %%-------------------------------------------------------------------- %% Authentication behavihour @@ -51,7 +51,7 @@ behaviour_info(_Other) -> -endif. %% @doc Password Hash --spec(passwd_hash(hash_type(), binary()) -> binary()). +-spec(passwd_hash(hash_type(), binary() | tuple()) -> binary()). passwd_hash(plain, Password) -> Password; passwd_hash(md5, Password) -> @@ -59,7 +59,11 @@ passwd_hash(md5, Password) -> passwd_hash(sha, Password) -> hexstring(crypto:hash(sha, Password)); passwd_hash(sha256, Password) -> - hexstring(crypto:hash(sha256, Password)). + hexstring(crypto:hash(sha256, Password)); +passwd_hash(pbkdf2,{Salt,Password,Macfun,Iterations,Dklen}) -> + {ok,Hexstring} = pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen), + hexstring(Hexstring). + hexstring(<>) -> iolist_to_binary(io_lib:format("~32.16.0b", [X])); From 25c25826a7690c78d3cb6e6fc03b9dbaad84b957 Mon Sep 17 00:00:00 2001 From: huangpengju <3310324470@qq.com> Date: Thu, 9 Mar 2017 09:28:28 +0800 Subject: [PATCH 2/4] Update Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 558d66180..e137d5e3f 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ dep_lager = git https://github.com/basho/lager master dep_esockd = git https://github.com/emqtt/esockd master dep_mochiweb = git https://github.com/emqtt/mochiweb dep_lager_syslog = git https://github.com/basho/lager_syslog -dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0 +dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0 ERLC_OPTS += +'{parse_transform, lager_transform}' From 78b0f6691019139fc2599b3e4773d4a6aaf1aa22 Mon Sep 17 00:00:00 2001 From: huangpengju <3310324470@qq.com> Date: Thu, 9 Mar 2017 09:29:03 +0800 Subject: [PATCH 3/4] Update Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e137d5e3f..6506d1fbe 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ dep_lager = git https://github.com/basho/lager master dep_esockd = git https://github.com/emqtt/esockd master dep_mochiweb = git https://github.com/emqtt/mochiweb dep_lager_syslog = git https://github.com/basho/lager_syslog -dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0 +dep_pbkdf2 = git https://github.com/comtihon/erlang-pbkdf2.git 2.0.0 ERLC_OPTS += +'{parse_transform, lager_transform}' From b5ff80499ad138d5bc545d5f352335be3e907813 Mon Sep 17 00:00:00 2001 From: huangpengju <3310324470@qq.com> Date: Thu, 9 Mar 2017 09:29:31 +0800 Subject: [PATCH 4/4] Update emqttd_auth_mod.erl --- src/emqttd_auth_mod.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/emqttd_auth_mod.erl b/src/emqttd_auth_mod.erl index 4ddf4c067..8f0b7405a 100644 --- a/src/emqttd_auth_mod.erl +++ b/src/emqttd_auth_mod.erl @@ -62,8 +62,7 @@ passwd_hash(sha256, Password) -> hexstring(crypto:hash(sha256, Password)); passwd_hash(pbkdf2,{Salt,Password,Macfun,Iterations,Dklen}) -> {ok,Hexstring} = pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen), - hexstring(Hexstring). - + pbkdf2:to_hex(Hexstring). hexstring(<>) -> iolist_to_binary(io_lib:format("~32.16.0b", [X]));