Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
chanzdevilz
Posts: 16
Registered: ‎06-06-2011
My Device: 8520

[Help] XMPP Digest-MD5

[ Edited ]

hi,

i'm developing messenger application. i'm using xmpp digest-md5. i have problem to reponse back to server when response send me a challenge.

i use http://web.archive.org/web/20050224191820/http://cataclysm.cx/wip/digest-md5-crash.html as reference

 to response server.

i didnt know why cant read server response. 

 

int offset = 0;
MD5Digest digest = new MD5Digest();
message = username+":"+host+":"+password;
digest.update(message.getBytes(charset));
byte[] buffer = new byte[digest.getDigestLength()];
digest.getDigest(buffer, offset);
message = getString(buffer);
digest.reset();
message = message+":"+nonce+":"+cnonce;
digest.update(message.getBytes(charset));

buffer = new byte[digest.getDigestLength()];
digest.getDigest(buffer, offset);
String ha1 = getString(buffer);

digest.reset();
message = "AUTHENTICATE:"+digestUri;
digest.update(message.getBytes(charset));
buffer = new byte[digest.getDigestLength()];
digest.getDigest(buffer, offset);
String ha2 = getString(buffer);

digest.reset();
message = ha1+":"+nonce+":"+nc+":"+qop+":"+ha2;
digest.update(message.getBytes(charset));
buffer = new byte[digest.getDigestLength()];
digest.getDigest(buffer, offset);
message = getString(buffer);

message = "username="+username+", realm="+host+", nonce="+nonce+", cnonce="+cnonce+", nc="+nc+", qop="+qop+", digest-uri="+digestUri+", response="+message+", charset="+charset;
message = new String(Base64OutputStream.encode(message.getBytes(), 0, message.getBytes().length, false, false),"UTF-8");
message = "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"+message+"</response>";

os.write(message.getBytes());
os.flush();

 

private static String getString(byte[] buffer){
	StringBuffer sb = new StringBuffer();
	for(int i=0; i < buffer.length ; i++){
		byte b = buffer[i];
		String hex = Integer.toHexString((int) 0x00FF & b);
		if(hex.length()==1){
			sb.append("0");
		}
		sb.append( hex );
	}	
	return sb.toString();
}