Over the weekend I’ve been busy writing an XMPP client from scratch. So far, I’ve got a basic console application connected to Facebook chat. No sending messages support yet, but the friends list, receiving messages and presence data are working quite well.
Its also connected successfully to Google Chat or Talk or whatever it’s called using TSL.
By far the hardest thing to implement was the digest authentication (DIGEST-MD5) mechanism used by Facebook (and many other hosts, Google supports PLAIN). The C# code for creating the response hash looks something like this:
MD5 md5 = MD5.Create();
byte[] a = ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}:{2}", Xmpp.UserName, Xmpp.UserDomain, Xmpp.Password));
byte[] HA1_1 = md5.ComputeHash(a);
byte[] HA1_2 = ASCIIEncoding.ASCII.GetBytes(string.Format(":{0}:{1}", elements["nonce"], cnonce));
byte[] HA1 = new byte[HA1_2.Length + HA1_1.Length];
HA1_1.CopyTo(HA1, 0);
HA1_2.CopyTo(HA1, HA1_1.Length);
HA1 = md5.ComputeHash(HA1);
byte[] HA2 = Md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(string.Format("AUTHENTICATE:xmpp/{0}",xmpp.UserDomain)));
byte[] b = ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}:{2}:{3}:{4}:{5}", ConvertToHexString(HA1), elements["nonce"], "00000001", cnonce, elements["qop"], ConvertToHexString(HA2)));
byte[] hash = md5.ComputeHash(b);
string res = ConvertToHexString(hash);
And the code to convert an array of bytes to a base 16 string:
public static string ConvertToHexString(byte[] inArray)
{
StringBuilder sb = new StringBuilder();
foreach (byte part in inArray)
{
sb.AppendFormat("{0:x2}", part);
}
return sb.ToString();
}
In the next few days I’ll write up a brief DIGEST-MD5 tutorial as I wasted quite a bit of time trying to piece it together from various resources and RFCs.
CAMERON says:
Medicamentspot.com International Legal RX Medications. Special Internet Prices (up to 40% off average US price). NO PRIOR PRESCRIPTION REQUIRED!…
Combivir@buy.online” rel=”nofollow”>.…
June 24, 2010, 1:56 pm