Created
July 10, 2014 00:38
-
-
Save jprystowsky/c33ff9b416c2db2d3500 to your computer and use it in GitHub Desktop.
This is a snippet of code I wrote while writing an API against the Mixpanel API. If you're building a raw http(s, actually -- thanks guys!) API request and you need to generate a signature for an IDictionary<string,string> of your query arguments, then this method will do it. (Simplified from a more generic implementation for readability; reimpl…
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.Collections.Generic; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Mixpanel.Security { | |
public class MixpanelApiAuthenticator { | |
private const string MIXPANEL_API_SECRET = @"Here's your secret, son"; | |
public string GetAuthenticationUriSubstring(IDictionary<string, string> queryArguments) { | |
return queryArguments | |
.Keys | |
.OrderBy(s => s) | |
.Aggregate(string.Empty, | |
(accum, s) => accum += string.Format("{0}={1}", s, queryArguments[s]), | |
s => { | |
var accumPlusApiSecret = string.Format("{0}{1}", s, MIXPANEL_API_SECRET); | |
var md5 = MD5.Create(); | |
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(accumPlusApiSecret)); | |
return string.Concat( | |
hash.Select(b => b.ToString("X2")) | |
) | |
.ToLowerInvariant(); | |
} | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment