Created
May 13, 2015 11:53
-
-
Save asmorger/b5c523a50dcd0a5e1e20 to your computer and use it in GitHub Desktop.
Nerdery .Net Challenge #2
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.Globalization; | |
| using System.Linq; | |
| using System.Security.Cryptography; | |
| using System.Text; | |
| using Newtonsoft.Json; | |
| namespace ConsoleApplication1 | |
| { | |
| public class Program | |
| { | |
| private const string SignatureSalt = "1234MySuperSecretKey4321"; | |
| private const string InputData = | |
| @"{'author_name': 'Robert Jordan', 'book_title': 'Knife of Dreams', 'series': 'The Wheel of Time, Book 11', 'publisher': 'Tor Fantasy', 'published_date': 'November 28, 2006' }"; | |
| private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1); | |
| public static void Main() | |
| { | |
| var queryStringValues = JsonConvert.DeserializeObject<Dictionary<string, string>>(InputData); | |
| var timestamp = (DateTime.UtcNow - UnixEpoch).TotalMilliseconds; | |
| queryStringValues.Add("timestamp", timestamp.ToString(CultureInfo.InvariantCulture)); | |
| var signature = GetSignature(queryStringValues); | |
| queryStringValues.Add("signature", signature); | |
| var finalQueryString = GetQueryStringText(queryStringValues); | |
| Console.Out.WriteLine(finalQueryString); | |
| } | |
| private static string GetQueryStringText(Dictionary<string, string> items) | |
| { | |
| var querystringPieces = items.Keys.OrderBy(x => x).Select(k => string.Concat(k, "=", items[k])); | |
| var querystring = string.Join("&", querystringPieces); | |
| return querystring; | |
| } | |
| private static string GetSignature(Dictionary<string, string> queryStringValues) | |
| { | |
| var querystring = GetQueryStringText(queryStringValues); | |
| var source = string.Concat(querystring, SignatureSalt); | |
| string output; | |
| using (var crypto = new SHA1CryptoServiceProvider()) | |
| { | |
| var buffer = Encoding.UTF8.GetBytes(source); | |
| var hash = crypto.ComputeHash(buffer); | |
| output = Convert.ToBase64String(hash); | |
| } | |
| return output; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment