/// <summary> /// Generates a MD5 signature for the Facebook session. /// </summary> /// <param name="secret"> /// The app secret. /// </param> /// <param name="dictionary"> /// The dictionary. /// </param> /// <returns> /// Returns the generated signature. /// </returns> /// <remarks> /// http://developers.facebook.com/docs/authentication/ /// </remarks> internal static string GenerateSessionSignature(string secret, IDictionary <string, object> dictionary) { if (string.IsNullOrEmpty(secret)) { throw new ArgumentNullException("secret"); } if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var payload = new StringBuilder(); // sort by the key and remove "sig" if present var parts = (from a in dictionary orderby a.Key where a.Key != "sig" select string.Format(CultureInfo.InvariantCulture, "{0}={1}", a.Key, a.Value)).ToList(); parts.ForEach(s => payload.Append(s)); payload.Append(secret); var hash = FacebookWebUtils.ComputerMd5Hash(Encoding.UTF8.GetBytes(payload.ToString())); var signature = new StringBuilder(); foreach (var h in hash) { signature.Append(h.ToString("x2", CultureInfo.InvariantCulture)); } return(signature.ToString()); }
public void ItShouldGenerateCorrectMd5Hash() { var input = System.Text.Encoding.UTF8.GetBytes("access_token=124973200873702|2.OAaqICOCk_B4sZNv59q8Yg__.3600.1295118000-100001327642026|vz4H9xjlRZPfg2quCv0XOM5g9_oexpires=1295118000secret=lddpssZCuPoEtjcDFcWtoA__session_key=2.OAaqICOCk_B4sZNv59q8Yg__.3600.1295118000-100001327642026uid=1000013276420263b4a872617be2ae1932baa1d4d240272"); var expected = new byte[] { 29, 149, 250, 75, 61, 250, 91, 38, 192, 28, 138, 200, 103, 109, 128, 184 }; var md5Hash = FacebookWebUtils.ComputerMd5Hash(input); Assert.True(expected.SequenceEqual(md5Hash)); }