/// <summary> /// Calculates the signature for an OAuth call. /// </summary> /// <param name="method">POST or GET method.</param> /// <param name="url">The URL the request will be sent to.</param> /// <param name="parameters">Parameters to be added to the signature.</param> /// <param name="tokenSecret">The token secret (either request or access) for generating the SHA-1 key.</param> /// <returns>Base64 encoded SHA-1 hash.</returns> public string OAuthCalculateSignature(string method, string url, Dictionary <string, string> parameters, string tokenSecret) { string baseString = ""; string key = ApiSecret + "&" + tokenSecret; byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key); #if !SILVERLIGHT SortedList <string, string> sorted = new SortedList <string, string>(); foreach (KeyValuePair <string, string> pair in parameters) { sorted.Add(pair.Key, pair.Value); } #else var sorted = parameters.OrderBy(p => p.Key); #endif StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sorted) { sb.Append(pair.Key); sb.Append("="); sb.Append(UtilityMethods.EscapeOAuthString(pair.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); baseString = method + "&" + UtilityMethods.EscapeOAuthString(url) + "&" + UtilityMethods.EscapeOAuthString(sb.ToString()); #if WindowsCE FlickrNet.Security.Cryptography.HMACSHA1 sha1 = new FlickrNet.Security.Cryptography.HMACSHA1(keyBytes); #else System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(keyBytes); #endif byte[] hashBytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(baseString)); string hash = Convert.ToBase64String(hashBytes); Debug.WriteLine("key = " + key); Debug.WriteLine("base = " + baseString); Debug.WriteLine("sig = " + hash); return(hash); }
/// <summary> /// Calculates the signature for an OAuth call. /// </summary> /// <param name="method">POST or GET method.</param> /// <param name="url">The URL the request will be sent to.</param> /// <param name="parameters">Parameters to be added to the signature.</param> /// <param name="tokenSecret">The token secret (either request or access) for generating the SHA-1 key.</param> /// <returns>Base64 encoded SHA-1 hash.</returns> public string OAuthCalculateSignature(string method, string url, Dictionary<string, string> parameters, string tokenSecret) { string baseString = ""; string key = ApiSecret + "&" + tokenSecret; byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key); #if !SILVERLIGHT SortedList<string, string> sorted = new SortedList<string, string>(); foreach (KeyValuePair<string, string> pair in parameters) { sorted.Add(pair.Key, pair.Value); } #else var sorted = parameters.OrderBy(p => p.Key); #endif StringBuilder sb = new StringBuilder(); foreach (KeyValuePair<string, string> pair in sorted) { sb.Append(pair.Key); sb.Append("="); sb.Append(UtilityMethods.EscapeOAuthString(pair.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); baseString = method + "&" + UtilityMethods.EscapeOAuthString(url) + "&" + UtilityMethods.EscapeOAuthString(sb.ToString()); #if WindowsCE FlickrNet.Security.Cryptography.HMACSHA1 sha1 = new FlickrNet.Security.Cryptography.HMACSHA1(keyBytes); #else System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(keyBytes); #endif byte[] hashBytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(baseString)); string hash = Convert.ToBase64String(hashBytes); Debug.WriteLine("key = " + key); Debug.WriteLine("base = " + baseString); Debug.WriteLine("sig = " + hash); return hash; }
public string OAuthCalculateSignature(string method, string url, Dictionary <string, string> parameters, string tokenSecret) { var baseString = ""; var key = Secret + "&" + tokenSecret; var keyBytes = Encoding.UTF8.GetBytes(key); #if !SILVERLIGHT var sorted = new SortedList <string, string>(); foreach (var pair in parameters) { sorted.Add(pair.Key, pair.Value); } #else var sorted = parameters.OrderBy(p => p.Key); #endif var sb = new StringBuilder(); foreach (var pair in sorted) { sb.Append(pair.Key); sb.Append("="); sb.Append(EscapeOAuthString(pair.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); baseString = method + "&" + EscapeOAuthString(url) + "&" + EscapeOAuthString(sb.ToString()); #if WindowsCE FlickrNet.Security.Cryptography.HMACSHA1 sha1 = new FlickrNet.Security.Cryptography.HMACSHA1(keyBytes); #else var sha1 = new HMACSHA1(keyBytes); #endif var hashBytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(baseString)); var hash = Convert.ToBase64String(hashBytes); return(hash); }