Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created April 11, 2013 13:11
Show Gist options
  • Select an option

  • Save chgeuer/5363250 to your computer and use it in GitHub Desktop.

Select an option

Save chgeuer/5363250 to your computer and use it in GitHub Desktop.
Create and check own SAS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HMac
{
class Program
{
static void Main(string[] args)
{
// Generate SAS Key on Server
byte[] key = Convert.FromBase64String("StorageKeyDFBASE64deadbeef==");
var hmac = new System.Security.Cryptography.HMACSHA256(key);
string url = "http://www.videost.cdn.com/access?customer=bill&video=fairies.wmv";
byte[] contentToBeSigned = Encoding.UTF8.GetBytes(url);
string computedSignatureOnSender = Convert.ToBase64String(hmac.ComputeHash(contentToBeSigned));
Console.WriteLine(computedSignatureOnSender);
var protectedUrl = string.Format("{0}&sig={1}", url, computedSignatureOnSender);
// Verify SAS Key on another server by re-calculating the hash over the URL
string toBeChecked = protectedUrl.Substring(0, protectedUrl.IndexOf("&sig="));
string sigFromUrl = protectedUrl.Substring(protectedUrl.IndexOf("&sig=") + 5);
Console.WriteLine(toBeChecked);
var hmacVerifier = new System.Security.Cryptography.HMACSHA256(key);
string recomputedSignatureOnReceiver = Convert.ToBase64String(hmacVerifier.ComputeHash(Encoding.UTF8.GetBytes(toBeChecked)));
bool isAllowed = (sigFromUrl == recomputedSignatureOnReceiver);
Console.WriteLine(isAllowed ? "gut" : "böse");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment