Skip to content

Instantly share code, notes, and snippets.

@urlbox
Last active December 13, 2015 20:38
Show Gist options
  • Save urlbox/4970990 to your computer and use it in GitHub Desktop.
Save urlbox/4970990 to your computer and use it in GitHub Desktop.
urlbox c#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public class Urlbox
{
public string urlGenerator(string url)
{
var urlboxApiKey = "xxx-xxx";
var urlboxApiSecret = "xxx-xxx";
var encodedUrl = System.Uri.EscapeDataString(url);
byte[] key = Encoding.UTF8.GetBytes(urlboxApiSecret);
var queryString = string.Format("url={0}", encodedUrl);
var uniqueHash = generateHash(queryString, key);
return string.Format("https://api.urlbox.io/v1/{0}/{1}/png?{2}", urlboxApiKey, uniqueHash, queryString );
}
public static string generateHash(string input, byte[] key)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.UTF8.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment