Created
May 15, 2012 21:07
-
-
Save bkizzy/2705156 to your computer and use it in GitHub Desktop.
Publish to AWS SNS Topic via C#, works with WP7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Net; | |
using System.Linq; | |
public class PublishAmazonSnsMsg | |
{ | |
public static Uri getRequestUrl(string amzsnshost, string TopicArn, string Message, string AWSAccessKeyId, string privatekey) | |
{ | |
string url; | |
string string_to_sign; | |
string cannqs; | |
List<string> qs = new List<string>(); | |
List<string> tojoin = new List<string>(); | |
Dictionary<string, string> parameters = new Dictionary<string, string>(); | |
HMACSHA256 hmacsha256; | |
byte[] keyByte; | |
byte[] messageBytes; | |
parameters.Add("TopicArn", TopicArn); | |
parameters.Add("Message", Message); | |
parameters.Add("Timestamp", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")); | |
parameters.Add("AWSAccessKeyId", AWSAccessKeyId); | |
parameters.Add("Action", "Publish"); | |
parameters.Add("SignatureVersion", "2"); | |
parameters.Add("SignatureMethod", "HmacSHA256"); | |
var klist = parameters.Keys.ToList(); | |
klist.Sort(StringComparer.Ordinal); | |
foreach (var key in klist) | |
{ | |
qs.Add(HttpUtility.UrlEncode(key) + "=" + UrlEncode(parameters[key])); | |
} | |
cannqs = string.Join("&", qs); | |
tojoin.Add("GET"); | |
tojoin.Add(amzsnshost); | |
tojoin.Add("/"); | |
tojoin.Add(cannqs); | |
string_to_sign = string.Join("\n", tojoin.ToArray()); | |
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); | |
keyByte = encoding.GetBytes(privatekey); | |
hmacsha256 = new HMACSHA256(keyByte); | |
messageBytes = encoding.GetBytes(string_to_sign); | |
var sig = System.Convert.ToBase64String(hmacsha256.ComputeHash(messageBytes)); | |
//System.Diagnostics.Debug.WriteLine(sig); | |
url = "http://" + amzsnshost + "/?" + cannqs + "&Signature=" + UrlEncode(sig); | |
//System.Diagnostics.Debug.WriteLine(url); | |
return new Uri(url); | |
} | |
protected static string UrlEncode(string value) | |
{ | |
StringBuilder result = new StringBuilder(); | |
foreach (char symbol in value) | |
{ | |
if (unreservedChars.IndexOf(symbol) != -1) | |
{ | |
result.Append(symbol); | |
} | |
else if (reservedChars.IndexOf(symbol) > -1) | |
{ | |
result.Append('%' + String.Format("{0:X2}", (int)symbol)); | |
} | |
else | |
{ | |
result.Append(Uri.EscapeUriString(symbol + "").ToUpper()); | |
} | |
} | |
return result.ToString(); | |
} | |
protected static string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_~"; | |
protected static string reservedChars = "=+:/"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment