82766c3dc2fc349bdd8edf8f9fc81b39b0b82e55
[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) 
22                 throws SAXException, IOException
23         {
24                 xml_ = XmlUtil.parse(is);
25         }
26         
27         public boolean iswriteaccess()
28                 throws SAXException
29         {
30                 try {
31                         return xml_.getElementsByTagName("iswriteaccess").item(0).getTextContent().equals("1"); 
32                 }
33                 catch (NullPointerException e) {
34                         throw new SAXException();
35                 }
36         }
37         
38         public String sid() 
39                 throws SAXException
40         {
41                 try {
42                         return Util.nonnull(xml_.getElementsByTagName("SID").item(0).getTextContent());
43                 }
44                 catch (NullPointerException e) {
45                         throw new SAXException();
46                 }
47         }
48         
49         public String challenge()
50                 throws SAXException
51         {
52                 try {
53                         return Util.nonnull(xml_.getElementsByTagName("Challenge").item(0).getTextContent());
54                 }
55                 catch (NullPointerException e) {
56                         throw new SAXException();
57                 }
58         }
59         
60         public String response(String password)
61                 throws SAXException
62         {
63                 try {
64                         String c = challenge();
65                         MessageDigest md = MessageDigest.getInstance("MD5");
66                         md.update((c + "-" + password).getBytes("UTF-16LE"));
67                         return c + "-" + new HexBinaryAdapter().marshal(md.digest()).toLowerCase();
68                 } catch (NoSuchAlgorithmException e) {
69                         throw new AssertionError("missing MD5 implementation");
70                 } catch (UnsupportedEncodingException e) {
71                         throw new AssertionError("missing UTF-16LE encoding");
72                 }
73         }
74 }