.Net Sample for Amazon Web Services (AWS) Request Authentication

The following code sample demonstrates how to calculate a request signature to sign authenticated requests to AWS.

 

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
 
namespace Devintelligence.Aws
{
    /// <summary>
    /// Generation authentication signatures for AWS Platform requests.
    /// </summary>
    public class AwsUtils
    {
        /// <summary>
        /// Computes RFC 2104-compliant HMAC signature.
        /// </summary>
        /// <param name="data">The data to be signed.</param>
        /// <param name="key">The signing key.</param>
        /// <returns>The Base64-encoded RFC 2104-compliant HMAC signature.</returns>
        public static string calculateRFC2104HMAC(string data, string key)
        {
            byte[] bData = Encoding.UTF8.GetBytes(data);
            byte[] bKey = Encoding.UTF8.GetBytes(key);
 
 
            HMACSHA1 hmac = new HMACSHA1(bKey);
            using(CryptoStream cryptoStream =
                      new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write) )
            {
                cryptoStream.Write(bData, 0, bData.Length);
            }
            return Convert.ToBase64String(hmac.Hash);
        }
 
 
 
 
 
        /// <summary>
        /// Gets the time stamp.
        /// </summary>
        /// <param name="dateTime">Date time.</param>
        /// <returns></returns>
        public static string GetTimeStamp(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
        }
 
    }
}


 
 
 

Leave a Reply