Skip to content

Instantly share code, notes, and snippets.

@marcguyer
Last active December 18, 2015 23:09

Revisions

  1. marcguyer revised this gist Jul 8, 2013. 1 changed file with 50 additions and 42 deletions.
    92 changes: 50 additions & 42 deletions cheddargetter-webhook.cs
    Original file line number Diff line number Diff line change
    @@ -1,51 +1,59 @@
    /// Grab The Authorization Header
    var authorizationHeader = request.Headers["X-CG-SIGNATURE"];

    /// Set The Input Stream The Beginning
    request.InputStream.Seek(0, SeekOrigin.Begin);
    using (var reader = new StreamReader(request.InputStream))
    {
    /// Read The Entire Body In
    var httpBody = reader.ReadToEnd();
    /// Get MD5 Hash of the Entire Body
    var md5String = CalculateMd5Hash(httpBody);
    /// Get SHA256 HMAC Hash of the MD5 hash using my secret key as the salt
    var sha256String = CalculateSha256Hash(md5String, ConfigurationManager.AppSettings["BillingRepositorySecretKey"]);
    /// Check Against The Authorization Header
    if (sha256String.ToLower() != authorizationHeader.ToLower())
    {
    throw new Domain.Exceptions.Exception();
    }
    if (string.IsNullOrEmpty(authorizationHeader))
    {
    throw new Domain.Exceptions.Exception();
    }

    public string CalculateMd5Hash(string input)
    {
    /// fire up a new MD5 creator
    var md5 = MD5.Create();
    /// get the byte array hash
    var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(input));
    request.InputStream.Seek(0, SeekOrigin.Begin);

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
    sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
    using (var reader = new StreamReader(request.InputStream))
    {
    // Read The Entire Body In
    var httpBody = reader.ReadToEnd();
    // Get Token
    var token = CalculateMd5Hash(httpBody);
    // Get SHA256 HMAC Hash of the MD5 hash using my secret key as the salt
    var sha256String = CalculateSha256Hash(_secretKey, token);
    // Check Against The Authorization Header
    if (sha256String != authorizationHeader)
    {
    throw new Domain.Exceptions.Exception();
    }
    }

    public string CalculateSha256Hash(string input, string secretKey)
    {
    /// Build A SHA256 Hash Creator Using My Secret Key as the key
    var hmac = new HMACSHA256(Encoding.ASCII.GetBytes(secretKey));
    /// get the byte array hash
    var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(input));

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
    sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
    public string CalculateMd5Hash(string input)
    {
    // fire up a new MD5 creator
    var md5 = MD5.Create();
    // convert input to a byte array
    var inputBytes = Encoding.ASCII.GetBytes(input);
    // get the byte array hash
    var hash = md5.ComputeHash(inputBytes);

    // convert the byte array to a string and return
    var sb = new StringBuilder();
    for (var i = 0; i < hash.Length; i++)
    {
    sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();
    }

    public string CalculateSha256Hash(string secretKey,string md5)
    {
    // Get The Byte Array of My Secret Key
    var secretKeyArray = Encoding.ASCII.GetBytes(secretKey);
    // Build A SHA256 Hash Creator Using My Secret Key as the key
    var hash = new HMACSHA256(secretKeyArray);
    var byteArray = hash.ComputeHash(Encoding.ASCII.GetBytes(md5));

    // convert the byte array to a string and return
    var sb = new StringBuilder();
    for (var i = 0; i < byteArray.Length; i++)
    {
    sb.Append(byteArray[i].ToString("x2"));
    }
    return sb.ToString();
    }
  2. marcguyer revised this gist Jun 27, 2013. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions cheddargetter-webhook.cs
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    /// Get MD5 Hash of the Entire Body
    var md5String = CalculateMd5Hash(httpBody);
    /// Get SHA256 HMAC Hash of the MD5 hash using my secret key as the salt
    var sha256String = CalculateSha256Hash(md5String, _secretKey);
    var sha256String = CalculateSha256Hash(md5String, ConfigurationManager.AppSettings["BillingRepositorySecretKey"]);
    /// Check Against The Authorization Header
    if (sha256String.ToLower() != authorizationHeader.ToLower())
    {
    @@ -22,10 +22,8 @@ public string CalculateMd5Hash(string input)
    {
    /// fire up a new MD5 creator
    var md5 = MD5.Create();
    /// convert input to a byte array
    var inputBytes = Encoding.ASCII.GetBytes(input);
    /// get the byte array hash
    var hash = md5.ComputeHash(inputBytes);
    var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(input));

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
    @@ -38,10 +36,10 @@ public string CalculateMd5Hash(string input)

    public string CalculateSha256Hash(string input, string secretKey)
    {
    /// Get The Byte Array of My Secret Key
    var secretKeyArray = Encoding.ASCII.GetBytes(secretKey);
    /// Build A SHA256 Hash Creator Using My Secret Key as the key
    var hash = new HMACSHA256(secretKeyArray);
    var hmac = new HMACSHA256(Encoding.ASCII.GetBytes(secretKey));
    /// get the byte array hash
    var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(input));

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
  3. marcguyer revised this gist Jun 25, 2013. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions cheddargetter-webhook.cs
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,14 @@

    public string CalculateMd5Hash(string input)
    {
    var md5 = MD5.Create();
    var inputBytes = Encoding.ASCII.GetBytes(input);
    /// fire up a new MD5 creator
    var md5 = MD5.Create();
    /// convert input to a byte array
    var inputBytes = Encoding.ASCII.GetBytes(input);
    /// get the byte array hash
    var hash = md5.ComputeHash(inputBytes);

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
    @@ -39,6 +43,7 @@ public string CalculateSha256Hash(string input, string secretKey)
    /// Build A SHA256 Hash Creator Using My Secret Key as the key
    var hash = new HMACSHA256(secretKeyArray);

    /// convert the byte array to a string and return
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
  4. marcguyer created this gist Jun 25, 2013.
    48 changes: 48 additions & 0 deletions cheddargetter-webhook.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    /// Grab The Authorization Header
    var authorizationHeader = request.Headers["X-CG-SIGNATURE"];

    /// Set The Input Stream The Beginning
    request.InputStream.Seek(0, SeekOrigin.Begin);
    using (var reader = new StreamReader(request.InputStream))
    {
    /// Read The Entire Body In
    var httpBody = reader.ReadToEnd();
    /// Get MD5 Hash of the Entire Body
    var md5String = CalculateMd5Hash(httpBody);
    /// Get SHA256 HMAC Hash of the MD5 hash using my secret key as the salt
    var sha256String = CalculateSha256Hash(md5String, _secretKey);
    /// Check Against The Authorization Header
    if (sha256String.ToLower() != authorizationHeader.ToLower())
    {
    throw new Domain.Exceptions.Exception();
    }
    }

    public string CalculateMd5Hash(string input)
    {
    var md5 = MD5.Create();
    var inputBytes = Encoding.ASCII.GetBytes(input);
    var hash = md5.ComputeHash(inputBytes);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
    sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
    }

    public string CalculateSha256Hash(string input, string secretKey)
    {
    /// Get The Byte Array of My Secret Key
    var secretKeyArray = Encoding.ASCII.GetBytes(secretKey);
    /// Build A SHA256 Hash Creator Using My Secret Key as the key
    var hash = new HMACSHA256(secretKeyArray);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
    sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
    }