Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 85 lines (77 sloc) 3.392 kb
29482d40 » nniclausse
2006-04-19 merge sip-digest patch (Jason Tucker)
1 %%%
2 %%% Created: Apr 2006 by Jason Tucker <jasonwtucker@gmail.com>
3 %%%
4 %%% Modified by Nicolas Niclausse
5 %%%
6 %%% This program is free software; you can redistribute it and/or modify
7 %%% it under the terms of the GNU General Public License as published by
8 %%% the Free Software Foundation; either version 2 of the License, or
9 %%% (at your option) any later version.
10 %%%
11 %%% This program is distributed in the hope that it will be useful,
12 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
13 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 %%% GNU General Public License for more details.
15 %%%
16 %%% You should have received a copy of the GNU General Public License
17 %%% along with this program; if not, write to the Free Software
18 %%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19 %%%
20 %%% In addition, as a special exception, you have the permission to
21 %%% link the code of this program with any library released under
22 %%% the EPL license and distribute linked combinations including
23 %%% the two.
24
25 -module(ts_digest).
26 -author('jasonwtucker@gmail.com').
27
28 -export([ digest/2, sip_digest/4,
29 md5hex/1, shahex/1, tohex/1
30 ]).
31
32 %%%----------------------------------------------------------------------
33 %%% Func: sip_digest/4
34 %%%----------------------------------------------------------------------
35 sip_digest(Nonce, Jid, Realm, Passwd) ->
36 HA1 = md5hex(Jid ++ ":" ++ Realm ++ ":" ++ Passwd),
37 HA2 = md5hex("REGISTER:" ++ Jid),
38 INTEGRITY = md5hex(Nonce ++ ":" ++ HA2),
39 HA3 = md5hex(HA1 ++ ":" ++ INTEGRITY),
40 {HA3,INTEGRITY}.
41
42 %%%----------------------------------------------------------------------
43 %%% Func: digest/2
44 %%% Computes XMPP digest password described in JEP-0078
45 %%%----------------------------------------------------------------------
46 digest(Sid, Passwd) ->
47 HA1 = shahex(Sid ++ Passwd),
48 {HA1}.
49
50 %%%----------------------------------------------------------------------
51 %%% Func: md5hex/1
52 %%%----------------------------------------------------------------------
53 md5hex(Clear) ->
54 tohex(binary_to_list(erlang:md5(Clear))).
55
56 %%%----------------------------------------------------------------------
57 %%% Func: shahex/1
58 %%%----------------------------------------------------------------------
59 shahex(Clear) ->
0bd633ee » nniclausse
2006-05-12 use crypto:start only if needed
60 ShaVal= case catch crypto:sha(Clear) of
61 {'EXIT',_} ->
62 crypto:start(),
63 crypto:sha(Clear);
64 Sha -> Sha
65 end,
66 tohex(binary_to_list(ShaVal)).
29482d40 » nniclausse
2006-04-19 merge sip-digest patch (Jason Tucker)
67
68 %%%----------------------------------------------------------------------
69 %%% Func: tohex/1
70 %%% Purpose: convert list of integers to hexadecimal string
71 %%%----------------------------------------------------------------------
72 tohex(A)->
73 Fun = fun(X)->
56050dbc » nniclausse
2008-01-05 use new funs in base64 and string modules for R12B compatibility (TSU…
74 ts_utils:to_lower(padhex(httpd_util:integer_to_hexlist(X)))
29482d40 » nniclausse
2006-04-19 merge sip-digest patch (Jason Tucker)
75 end,
76 lists:flatten( lists:map(Fun, A) ).
77
78 %%%----------------------------------------------------------------------
79 %%% Func: padhex/1
80 %%% Purpose: needed because httpd_util:integer_to_hexlist returns hex
81 %%% values <10 as only 1 character, ie. "0F" is simply returned as
82 %%% "F". For our digest, we need these leading zeros to be present.
83 %%% ----------------------------------------------------------------------
239c0c55 » nniclausse
2006-05-19 add _ to unused variables
84 padhex(S=[_Char]) -> "0" ++ S;
29482d40 » nniclausse
2006-04-19 merge sip-digest patch (Jason Tucker)
85 padhex(String) -> String.
Something went wrong with that request. Please try again.