1 18 19 package sync4j.framework.notification; 20 21 import java.security.MessageDigest ; 22 import sync4j.framework.tools.Base64; 23 import java.security.NoSuchAlgorithmException ; 24 25 26 39 public class DigestNotificationMessage { 40 41 private static final String ALGORITHM_NAME = "MD5"; 43 private static final String SEPARATOR = ":"; 44 45 private String serverId; 47 private String serverPw; 48 private byte[] nonce; 49 50 52 public DigestNotificationMessage() {} 53 54 61 public DigestNotificationMessage(final String serverId, 62 final String serverPw, 63 final byte[] nonce) { 64 setServerId(serverId); 65 setServerPw(serverPw); 66 setNonce (nonce ); 67 } 68 69 75 public String getServerId() { 76 return serverId; 77 } 78 79 84 public void setServerId(String serverId) { 85 this.serverId = serverId; 86 } 87 88 93 public String getServerPw() { 94 return serverPw; 95 } 96 97 102 public void setServerPw(String serverPw) { 103 this.serverPw = serverPw; 104 } 105 106 111 public byte[] getNonce() { 112 return nonce; 113 } 114 115 120 public void setNonce(byte[] nonce) { 121 this.nonce = (nonce == null) ? new byte[0] : nonce; 122 } 123 124 134 public byte[] computeDigestMessage(byte[] triggerMessage) throws NoSuchAlgorithmException { 135 136 String cred = serverId + SEPARATOR + serverPw; 137 138 MessageDigest 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 digestTriggerMessage = md.digest(triggerMessage); 149 150 b64DigestTriggerMessage = Base64.encode(digestTriggerMessage); 152 153 md.reset(); 154 155 digestCred = md.digest(cred.getBytes()); 157 158 b64DigestCred = Base64.encode(digestCred); 160 161 md.reset(); 162 163 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 |