Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. Authorization: VWS {provision_access_key}:{Signature}
  2.  
  3. Signature = Base64(HMAC-SHA1(server_secret_key, StringToSign ) ) ;
  4. StringToSign =
  5. HTTP-Verb + "n" +
  6. Content-MD5 + "n" +
  7. Content-Type + "n" +
  8. Date + "n" +
  9. Request-Path;
  10.  
  11. class SignatureBuilder
  12. {
  13. public string tmsSignature(HttpWebRequest request, string seckretKey)
  14. {
  15.  
  16. string method = request.Method;
  17. string ContentType = "";
  18. string HexDigest = "d41d8cd98f00b204e9800998ecf8427e";
  19. if (method.Equals("GET", StringComparison.OrdinalIgnoreCase) || method.Equals("DELETE", StringComparison.OrdinalIgnoreCase))
  20. { }
  21. else if (method.Equals("POST", StringComparison.OrdinalIgnoreCase) || method.Equals("PUT", StringComparison.OrdinalIgnoreCase))
  22. {
  23. ContentType = "application/json";
  24. HexDigest = contentMD5(request);
  25. }
  26. else
  27. { System.Console.WriteLine("Error: invalid content type passed to Sig Builder"); }
  28. string dateValue = request.Date.ToUniversalTime().ToString();
  29. string requestPath = request.RequestUri.ToString();
  30. string toDigest = method + "n" + HexDigest + "n" + ContentType + "n" + dateValue + "n" + requestPath;
  31. string shaHashed = "";
  32. try
  33. {
  34. shaHashed = CalculateRFC2104HMAC(seckretKey, toDigest);
  35. }
  36. catch (Exception ex)
  37. {
  38. Console.WriteLine(ex.ToString());
  39. }
  40. return shaHashed;
  41. }
  42.  
  43. private string contentMD5(HttpWebRequest request)
  44. {
  45. const int BUFFER_MAX_SIZE = 1024;
  46. byte[] buffer = new byte[BUFFER_MAX_SIZE];
  47. using (Stream stream = request.GetRequestStream())
  48. {
  49. stream.Write(buffer, 0, BUFFER_MAX_SIZE);
  50. }
  51. string resultHash = BitConverter.ToString(MD5.Create().ComputeHash(buffer)).Replace("-", String.Empty).ToLower();
  52. return resultHash;
  53. }
  54. public static string CalculateRFC2104HMAC(string key, string data)
  55. {
  56.  
  57. UTF8Encoding utf8 = new UTF8Encoding();
  58. string result = "";
  59. byte[] buffer = new byte[256];
  60. try
  61. {
  62. MemoryStream stream = new MemoryStream(buffer);
  63. stream.Write(utf8.GetBytes(data), 0, data.Length);
  64. var crypto = HMACSHA1.Create();
  65. crypto.Key = Encoding.UTF8.GetBytes(key);
  66. var buff = crypto.ComputeHash(buffer);
  67. result = BitConverter.ToString(buff).ToLower().Replace("-", string.Empty);
  68. }
  69. catch (Exception ex)
  70. {
  71. Console.WriteLine(ex.ToString());
  72. }
  73. return result;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement