diff --git a/src/emqttd_auth_mod.erl b/src/emqttd_auth_mod.erl index 30fe1c17d..a52edac28 100644 --- a/src/emqttd_auth_mod.erl +++ b/src/emqttd_auth_mod.erl @@ -27,6 +27,10 @@ -include("emqttd.hrl"). +-export([passwd_hash/2]). + +-type hash_type() :: plain | md5 | sha | sha256. + %%%============================================================================= %%% Auth behavihour %%%============================================================================= @@ -53,3 +57,21 @@ behaviour_info(_Other) -> -endif. +%% @doc Password Hash +-spec passwd_hash(hash_type(), binary()) -> 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)). + +hexstring(<>) -> + iolist_to_binary(io_lib:format("~32.16.0b", [X])); +hexstring(<>) -> + iolist_to_binary(io_lib:format("~40.16.0b", [X])); +hexstring(<>) -> + iolist_to_binary(io_lib:format("~64.16.0b", [X])). +