support pbkdf2

This commit is contained in:
huangpj 2017-03-08 18:01:59 +08:00
parent 331d016c8e
commit fba79b3e25
2 changed files with 9 additions and 4 deletions

View File

@ -4,7 +4,7 @@ PROJECT_VERSION = 2.1.0
NO_AUTOPATCH = cuttlefish 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_gproc = git https://github.com/uwiger/gproc
dep_getopt = git https://github.com/jcomellas/getopt v0.8.2 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_esockd = git https://github.com/emqtt/esockd master
dep_mochiweb = git https://github.com/emqtt/mochiweb dep_mochiweb = git https://github.com/emqtt/mochiweb
dep_lager_syslog = git https://github.com/basho/lager_syslog 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}' ERLC_OPTS += +'{parse_transform, lager_transform}'

View File

@ -22,7 +22,7 @@
-export([passwd_hash/2]). -export([passwd_hash/2]).
-type(hash_type() :: plain | md5 | sha | sha256). -type(hash_type() :: plain | md5 | sha | sha256 | pbkdf2).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Authentication behavihour %% Authentication behavihour
@ -51,7 +51,7 @@ behaviour_info(_Other) ->
-endif. -endif.
%% @doc Password Hash %% @doc Password Hash
-spec(passwd_hash(hash_type(), binary()) -> binary()). -spec(passwd_hash(hash_type(), binary() | tuple()) -> binary()).
passwd_hash(plain, Password) -> passwd_hash(plain, Password) ->
Password; Password;
passwd_hash(md5, Password) -> passwd_hash(md5, Password) ->
@ -59,7 +59,11 @@ passwd_hash(md5, Password) ->
passwd_hash(sha, Password) -> passwd_hash(sha, Password) ->
hexstring(crypto:hash(sha, Password)); hexstring(crypto:hash(sha, Password));
passwd_hash(sha256, 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(<<X:128/big-unsigned-integer>>) -> hexstring(<<X:128/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~32.16.0b", [X])); iolist_to_binary(io_lib:format("~32.16.0b", [X]));