Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing POST form in C# for uploading to Amazon S3

Tags:

c#

amazon-s3

I am having trouble signing my policy documents for the Amazon S3.

There are examples on how to do it in Ruby, Java, and Python, but when I try to do it in C#, it's not working out. I keep getting an invalid signature, and I'm not sure where I'm going wrong. http://aws.amazon.com/articles/1434

Can anyone provide an example like those in the article, except for C#?

Thanks.

like image 401
apexdodge Avatar asked Jan 20 '23 00:01

apexdodge


1 Answers

Solved it for anyone else who runs into the same problem.

class Program
{
    static string secretKey = "Removed";

    static void Main(string[] args)
    {
        string policyStr = @"{""expiration"": ""2012-01-01T12:00:00.000Z"",""conditions"": [{""bucket"": ""<bucket>"" },{""acl"": ""public-read"" },[""eq"", ""$key"", ""<filename>""],[""starts-with"", ""$Content-Type"", ""image/""],]}";

        GetSig(policyStr);
    }

    static void GetSig(string policyStr)
    {
        string b64Policy = Convert.ToBase64String(Encoding.ASCII.GetBytes(policyStr));

        byte[] b64Key = Encoding.ASCII.GetBytes(secretKey);
        HMACSHA1 hmacSha1 = new HMACSHA1(b64Key);
        Console.WriteLine(policyStr);
        Console.WriteLine(b64Policy);
        Console.WriteLine();
        Console.WriteLine(
            Convert.ToBase64String(hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(b64Policy))));

        Console.ReadKey();
    }
}
like image 84
apexdodge Avatar answered Feb 03 '23 23:02

apexdodge