Created
April 11, 2013 13:11
-
-
Save chgeuer/5363250 to your computer and use it in GitHub Desktop.
Create and check own SAS
This file contains hidden or 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.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