Difference between revisions of "Template:Json API HMAC Block- CSharp"
From Updox API
(Created page with " public static void setHmacHeaders(HttpWebRequest request) { string applicationId = "vendor-id"; string applicationPassword = "vendor-pa...") |
|||
(6 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | + | ||
+ | |||
+ | public void setHmacHeaders(HttpWebRequest request) | ||
{ | { | ||
string applicationId = "vendor-id"; | string applicationId = "vendor-id"; | ||
string applicationPassword = "vendor-password"; | string applicationPassword = "vendor-password"; | ||
+ | string accountId = "accountId"; | ||
+ | string userId = "userId"; | ||
string apiSecret = "vendor-private-secret-key"; | string apiSecret = "vendor-private-secret-key"; | ||
− | + | ||
// Create the date string | // Create the date string | ||
− | String timestampString = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss | + | String timestampString = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " (GMT)" |
− | + | ||
// Build authentication string | // Build authentication string | ||
− | string message = applicationId + ":" + applicationPassword + ":" + timestampString; | + | string message = |
− | + | applicationId + ":" + | |
− | // Create the athentication hash | + | applicationPassword + ":" + |
− | string auth = "HMAC | + | accountId + ":" + |
− | + | userId + ":" + | |
+ | timestampString; | ||
+ | |||
+ | // Create the athentication hash - note the intentional <space> after HMAC | ||
+ | string auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); // | ||
+ | |||
// Set the request headers | // Set the request headers | ||
request.Headers.Add("updox-timestamp", timestampString); | request.Headers.Add("updox-timestamp", timestampString); | ||
request.Headers.Add("Authorization", auth); | request.Headers.Add("Authorization", auth); | ||
} | } | ||
− | + | ||
− | public | + | public string hmacSha1ToBase64(String value, String key) |
{ | { | ||
// Get an hmac_sha1 key from the raw key bytes | // Get an hmac_sha1 key from the raw key bytes | ||
byte[] keyBytes = Encoding.UTF8.GetBytes(key); | byte[] keyBytes = Encoding.UTF8.GetBytes(key); | ||
byte[] valueBytes = Encoding.UTF8.GetBytes(value); | byte[] valueBytes = Encoding.UTF8.GetBytes(value); | ||
− | + | ||
− | + | ||
// Get an hmac_sha1 Mac instance and initialize with the signing key | // Get an hmac_sha1 Mac instance and initialize with the signing key | ||
HMACSHA1 myhmacsha1 = new HMACSHA1(); | HMACSHA1 myhmacsha1 = new HMACSHA1(); | ||
− | + | ||
myhmacsha1.Key = keyBytes; | myhmacsha1.Key = keyBytes; | ||
− | + | ||
byte[] final = myhmacsha1.ComputeHash(valueBytes); | byte[] final = myhmacsha1.ComputeHash(valueBytes); | ||
− | + | ||
// Covert array of Hex bytes to a String | // Covert array of Hex bytes to a String | ||
return Convert.ToBase64String(final); | return Convert.ToBase64String(final); | ||
} | } |
Latest revision as of 15:37, 5 February 2014
public void setHmacHeaders(HttpWebRequest request) { string applicationId = "vendor-id"; string applicationPassword = "vendor-password"; string accountId = "accountId"; string userId = "userId"; string apiSecret = "vendor-private-secret-key"; // Create the date string String timestampString = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " (GMT)" // Build authentication string string message = applicationId + ":" + applicationPassword + ":" + accountId + ":" + userId + ":" + timestampString; // Create the athentication hash - note the intentional <space> after HMAC string auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); // // Set the request headers request.Headers.Add("updox-timestamp", timestampString); request.Headers.Add("Authorization", auth); } public string hmacSha1ToBase64(String value, String key) { // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] valueBytes = Encoding.UTF8.GetBytes(value); // Get an hmac_sha1 Mac instance and initialize with the signing key HMACSHA1 myhmacsha1 = new HMACSHA1(); myhmacsha1.Key = keyBytes; byte[] final = myhmacsha1.ComputeHash(valueBytes); // Covert array of Hex bytes to a String return Convert.ToBase64String(final); }