add passwd_hash/2 api
This commit is contained in:
parent
b5c514b39c
commit
4934bf54c0
|
@ -27,6 +27,10 @@
|
||||||
|
|
||||||
-include("emqttd.hrl").
|
-include("emqttd.hrl").
|
||||||
|
|
||||||
|
-export([passwd_hash/2]).
|
||||||
|
|
||||||
|
-type hash_type() :: plain | md5 | sha | sha256.
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
%%% Auth behavihour
|
%%% Auth behavihour
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
|
@ -53,3 +57,21 @@ behaviour_info(_Other) ->
|
||||||
|
|
||||||
-endif.
|
-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(<<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])).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue