Move the passwd_hash/2 function to emqx-passwd project

This commit is contained in:
Feng Lee 2018-07-18 23:29:20 +08:00
parent b110a154ef
commit ca4cdfe4ee
1 changed files with 0 additions and 34 deletions

View File

@ -16,10 +16,6 @@
-include("emqx.hrl"). -include("emqx.hrl").
-export([passwd_hash/2]).
-type(hash_type() :: plain | md5 | sha | sha256 | pbkdf2 | bcrypt).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Authentication behavihour %% Authentication behavihour
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -46,33 +42,3 @@ behaviour_info(_Other) ->
-endif. -endif.
%% @doc Password Hash
-spec(passwd_hash(hash_type(), binary() | tuple()) -> binary()).
passwd_hash(plain, Password) ->
Password;
passwd_hash(md5, Password) ->
hexstring(crypto:hash(md5, Password));
passwd_hash(sha, Password) ->
hexstring(crypto:hash(sha, Password));
passwd_hash(sha256, Password) ->
hexstring(crypto:hash(sha256, Password));
passwd_hash(pbkdf2, {Salt, Password, Macfun, Iterations, Dklen}) ->
case pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen) of
{ok, Hexstring} -> pbkdf2:to_hex(Hexstring);
{error, Error} ->
emqx_logger:error("[AuthMod] PasswdHash with pbkdf2 error:~p", [Error]), <<>>
end;
passwd_hash(bcrypt, {Salt, Password}) ->
case bcrypt:hashpw(Password, Salt) of
{ok, HashPassword} -> list_to_binary(HashPassword);
{error, Error}->
emqx_logger:error("[AuthMod] PasswdHash with bcrypt error:~p", [Error]), <<>>
end.
hexstring(<<X:128/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~32.16.0b", [X]));
hexstring(<<X:160/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~40.16.0b", [X]));
hexstring(<<X:256/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~64.16.0b", [X])).