KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > notification > DigestNotificationMessage


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.notification;
20
21 import java.security.MessageDigest JavaDoc;
22 import sync4j.framework.tools.Base64;
23 import java.security.NoSuchAlgorithmException JavaDoc;
24
25
26 /**
27  * Represents the digest of the Notification message in according with the SyncML DM specification.
28  * <p>
29  * Compute the digest message in the following form:
30  * <p>
31  * <ui>Digest = H(B64(H(serverId:pw)):nonce:B64(H(trigger)))</ui>
32  * <p>
33  * using MD5 algorithm.
34  *
35  * @author Stefano Nichele @ Funambol
36  *
37  * @version $Id: DigestNotificationMessage.java,v 1.1 2005/05/16 17:32:55 nichele Exp $
38  */

39 public class DigestNotificationMessage {
40
41     // --------------------------------------------------------------- Constants
42
private static final String JavaDoc ALGORITHM_NAME = "MD5";
43     private static final String JavaDoc SEPARATOR = ":";
44
45     // -------------------------------------------------------------- Properties
46
private String JavaDoc serverId;
47     private String JavaDoc serverPw;
48     private byte[] nonce;
49
50     // ------------------------------------------------------------ Constructors
51

52     public DigestNotificationMessage() {}
53
54     /**
55      * Creates a new DigestNotificationMessage object with the given parameters
56      *
57      * @param serverId
58      * @param serverPw
59      * @param nonce
60      */

61     public DigestNotificationMessage(final String JavaDoc serverId,
62                                      final String JavaDoc serverPw,
63                                      final byte[] nonce) {
64         setServerId(serverId);
65         setServerPw(serverPw);
66         setNonce (nonce );
67     }
68
69     // ---------------------------------------------------------- Public methods
70
/**
71      * Gets the server identifier
72      *
73      * @return the server identifier
74      */

75     public String JavaDoc getServerId() {
76         return serverId;
77     }
78
79     /**
80      * Sets the server identifier
81      *
82      * @param the server identifier
83      */

84     public void setServerId(String JavaDoc serverId) {
85         this.serverId = serverId;
86     }
87
88     /**
89      * Gets the server password
90      *
91      * @return the server password
92      */

93     public String JavaDoc getServerPw() {
94         return serverPw;
95     }
96
97     /**
98      * Sets the server password
99      *
100      * @param the server password
101      */

102     public void setServerPw(String JavaDoc serverPw) {
103         this.serverPw = serverPw;
104     }
105
106     /**
107      * Gets the nonce
108      *
109      * @return the nonce
110      */

111     public byte[] getNonce() {
112         return nonce;
113     }
114
115     /**
116      * Sets the nonce
117      *
118      * @param the nonce; if null, the property nonce is set to an empty array
119      */

120     public void setNonce(byte[] nonce) {
121         this.nonce = (nonce == null) ? new byte[0] : nonce;
122     }
123
124     /**
125      * Compute the digest for the triggerMessage.
126      * <p>
127      * The digest message is calculate as:
128      * <p>
129      * Digest = H(B64(H(serverId:pw)):nonce:B64(H(trigger)))
130      * @param triggerMessage the message of which calculate the digest
131      * @throws NoSuchAlgorithmException if MD5 algorithm is not available
132      * @return byte[] the digest messge
133      */

134     public byte[] computeDigestMessage(byte[] triggerMessage) throws NoSuchAlgorithmException JavaDoc {
135
136         String JavaDoc cred = serverId + SEPARATOR + serverPw;
137
138         MessageDigest JavaDoc md = MessageDigest.getInstance(ALGORITHM_NAME);
139
140         byte[] digestTriggerMessage = null;
141         byte[] b64DigestTriggerMessage = null;
142
143         byte[] digestCred = null;
144         byte[] b64DigestCred = null;
145         byte[] digest = null;
146
147         // H(trigger)
148
digestTriggerMessage = md.digest(triggerMessage);
149
150         // B64(H(trigger))
151
b64DigestTriggerMessage = Base64.encode(digestTriggerMessage);
152
153         md.reset();
154
155         // H(serverId:pw)
156
digestCred = md.digest(cred.getBytes());
157
158         // B64(H(serverId:pw))
159
b64DigestCred = Base64.encode(digestCred);
160
161         md.reset();
162
163         // Creates a unique buffer containing the bytes to digest
164
//
165
byte[] buf = new byte[b64DigestCred.length + 2 + nonce.length + b64DigestTriggerMessage.length];
166
167         System.arraycopy(b64DigestCred, 0, buf, 0, b64DigestCred.length);
168         buf[b64DigestCred.length] = (byte)':';
169         System.arraycopy(nonce, 0, buf, b64DigestCred.length+1, nonce.length);
170         buf[b64DigestCred.length + nonce.length + 1] = (byte)':';
171         System.arraycopy(b64DigestTriggerMessage, 0, buf, b64DigestCred.length + nonce.length + 2, b64DigestTriggerMessage.length);
172
173         digest = md.digest(buf);
174
175         return digest;
176     }
177
178 }
Popular Tags