c5ec76bf1462b7dd76e71194b0898e2a749010c1
[jpim.git] / src / de / j32 / avmfritz / LoginXML.java
1 package de.j32.avmfritz;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UnsupportedEncodingException;
6 import java.security.MessageDigest;
7 import java.security.NoSuchAlgorithmException;
8
9 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
10
11 import org.w3c.dom.Document;
12 import org.xml.sax.SAXException;
13
14 import de.j32.util.Util;
15 import de.j32.util.XmlUtil;
16
17 public class LoginXML
18 {
19     Document xml_;
20
21     public LoginXML(InputStream is) throws SAXException, IOException
22     {
23         xml_ = XmlUtil.parse(is);
24     }
25
26     public boolean iswriteaccess() throws SAXException
27     {
28         try {
29             return xml_.getElementsByTagName("iswriteaccess").item(0)
30                     .getTextContent().equals("1");
31         }
32         catch (NullPointerException e) {
33             throw new SAXException();
34         }
35     }
36
37     public String sid() throws SAXException
38     {
39         try {
40             return Util.nonnull(xml_.getElementsByTagName("SID").item(0)
41                     .getTextContent());
42         }
43         catch (NullPointerException e) {
44             throw new SAXException();
45         }
46     }
47
48     public String challenge() throws SAXException
49     {
50         try {
51             return Util.nonnull(xml_.getElementsByTagName("Challenge").item(0)
52                     .getTextContent());
53         }
54         catch (NullPointerException e) {
55             throw new SAXException();
56         }
57     }
58
59     public String response(String password) throws SAXException
60     {
61         try {
62             String c = challenge();
63             MessageDigest md = MessageDigest.getInstance("MD5");
64             md.update((c + "-" + password).getBytes("UTF-16LE"));
65             return c + "-"
66                     + new HexBinaryAdapter().marshal(md.digest()).toLowerCase();
67         }
68         catch (NoSuchAlgorithmException e) {
69             throw new AssertionError("missing MD5 implementation");
70         }
71         catch (UnsupportedEncodingException e) {
72             throw new AssertionError("missing UTF-16LE encoding");
73         }
74     }
75 }