Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Authorization: VWS {provision_access_key}:{Signature}
- Signature = Base64(HMAC-SHA1(server_secret_key, StringToSign ) ) ;
- StringToSign =
- HTTP-Verb + "n" +
- Content-MD5 + "n" +
- Content-Type + "n" +
- Date + "n" +
- Request-Path;
- class SignatureBuilder
- {
- public string tmsSignature(HttpWebRequest request, string seckretKey)
- {
- string method = request.Method;
- string ContentType = "";
- string HexDigest = "d41d8cd98f00b204e9800998ecf8427e";
- if (method.Equals("GET", StringComparison.OrdinalIgnoreCase) || method.Equals("DELETE", StringComparison.OrdinalIgnoreCase))
- { }
- else if (method.Equals("POST", StringComparison.OrdinalIgnoreCase) || method.Equals("PUT", StringComparison.OrdinalIgnoreCase))
- {
- ContentType = "application/json";
- HexDigest = contentMD5(request);
- }
- else
- { System.Console.WriteLine("Error: invalid content type passed to Sig Builder"); }
- string dateValue = request.Date.ToUniversalTime().ToString();
- string requestPath = request.RequestUri.ToString();
- string toDigest = method + "n" + HexDigest + "n" + ContentType + "n" + dateValue + "n" + requestPath;
- string shaHashed = "";
- try
- {
- shaHashed = CalculateRFC2104HMAC(seckretKey, toDigest);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- return shaHashed;
- }
- private string contentMD5(HttpWebRequest request)
- {
- const int BUFFER_MAX_SIZE = 1024;
- byte[] buffer = new byte[BUFFER_MAX_SIZE];
- using (Stream stream = request.GetRequestStream())
- {
- stream.Write(buffer, 0, BUFFER_MAX_SIZE);
- }
- string resultHash = BitConverter.ToString(MD5.Create().ComputeHash(buffer)).Replace("-", String.Empty).ToLower();
- return resultHash;
- }
- public static string CalculateRFC2104HMAC(string key, string data)
- {
- UTF8Encoding utf8 = new UTF8Encoding();
- string result = "";
- byte[] buffer = new byte[256];
- try
- {
- MemoryStream stream = new MemoryStream(buffer);
- stream.Write(utf8.GetBytes(data), 0, data.Length);
- var crypto = HMACSHA1.Create();
- crypto.Key = Encoding.UTF8.GetBytes(key);
- var buff = crypto.ComputeHash(buffer);
- result = BitConverter.ToString(buff).ToLower().Replace("-", string.Empty);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement