Skip to content

Instantly share code, notes, and snippets.

@simonmcmanus
Forked from anonymous/GenerateHash
Last active August 29, 2015 13:57
Show Gist options
  • Save simonmcmanus/9917317 to your computer and use it in GitHub Desktop.
Save simonmcmanus/9917317 to your computer and use it in GitHub Desktop.
public class GenerateHash
{
private string key;
private string secret;
public GenerateHash(string key, string secret)
{
this.key = key;
this.secret = secret;
}
public int GetEpoch()
{
return (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
public string GetAuthorizationHeader()
{
string epoch = GetEpoch().ToString();
string stringTosign = string.Concat(key, epoch);
byte[] secretBytes = Encoding.ASCII.GetBytes(secret);
using (HMACSHA256 myhmacsha1 = new HMACSHA256(secretBytes))
{
byte[] byteArray = Encoding.ASCII.GetBytes(stringTosign);
byte[] hashValue = myhmacsha1.ComputeHash(byteArray);
string hashToBase64 = Convert.ToBase64String(hashValue);
string keyHashEpoch = string.Format("{0}:{1}:{2}", key, hashToBase64, epoch);
return string.Concat("A5-API ", keyHashEpoch);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment