/
fabric_auth.erl
64 lines (51 loc) · 1.4 KB
/
fabric_auth.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-module(fabric_auth).
% Copyright: Witold Baryluk, 2010
% License: Public Domain
% for auth
-export([make_challenge/2, make_response/3, is_response_correct/4]).
% auth - simple authentification (based slightly on Joe's lib_chan_auth)
random_string(N) ->
random_seed(),
random_string(N, []).
random_string(0, D) -> D;
random_string(N, D) ->
random_string(N-1, [random:uniform(26)-1+$a | D]).
random_seed() ->
{_,_,X1} = erlang:now(),
X = X1 + 1,
{H,M,S} = time(),
put(random_seed, {H*X rem 32767, M*X rem 32767, S*X rem 32767}).
shex(0) -> $0;
shex(1) -> $1;
shex(2) -> $2;
shex(3) -> $3;
shex(4) -> $4;
shex(5) -> $5;
shex(6) -> $6;
shex(7) -> $7;
shex(8) -> $8;
shex(9) -> $9;
shex(10) -> $a;
shex(11) -> $b;
shex(12) -> $c;
shex(13) -> $d;
shex(14) -> $e;
shex(15) -> $f.
dhex(D) when D < 256 ->
[shex(D div 16), shex(D rem 16) ].
md5(X) ->
MBin = erlang:md5(X),
{MBin, lists:flatten([ dhex(D) || D <- binary_to_list(MBin) ])}.
make_challenge(_Name, _Secret) ->
{_Md5_Bin, Md5_Hex} = md5(random_string(32)),
{Md5_Hex, void}.
make_response(Challenge, _Name, Secret) ->
{_Md5_Bin, Md5_Hex} = md5(Challenge ++ Secret),
Md5_Hex.
is_response_correct(Challenge, Name, Response, Secret) when length(Challenge) > 20, length(Response) > 20 ->
ValidResponse = make_response(Challenge, Name, Secret),
% io:format("~p~n", [{valid_response, ValidResponse}]),
case Response of
ValidResponse -> true;
_ -> false
end.