From fba79b3e25cca674aac783fa57428b23bb5b7135 Mon Sep 17 00:00:00 2001 From: huangpj Date: Wed, 8 Mar 2017 18:01:59 +0800 Subject: [PATCH] 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]));