SkyNetAuth.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using static System.Net.WebRequestMethods;
  6. namespace Vinno.vCloud.XunFeiAudio
  7. {
  8. public class SkyNetAuth
  9. {
  10. //编码,使用UTF8
  11. private static Encoding CHARSET_UTF8 = Encoding.UTF8;
  12. //加密
  13. private static string DIGEST_NAME = "SHA-256";
  14. private static string AUTH_KEY = "Authorization";
  15. public static string generateAuthUrl(String apiKey, String apiSecret, String url)
  16. {
  17. AuthorizationData authorizationData = assemble(Http.Get, url, apiKey, apiSecret, null, null);
  18. string authBase = Convert.ToBase64String(CHARSET_UTF8.GetBytes(authorizationData.Authorization));
  19. authBase = string.Format("{0}?{1}={2}&host={3}&date={4}", url, AUTH_KEY.ToLower(), UrlEncode(authBase, Encoding.UTF8),
  20. UrlEncode(authorizationData.Host, Encoding.UTF8), UrlEncode(authorizationData.Date, Encoding.UTF8));
  21. return authBase;
  22. }
  23. public static string generateAuthUrl(String apiKey, String apiSecret, String url, string body, String apiKeyKey)
  24. {
  25. AuthorizationData authorizationData = assemble(Http.Get, url, apiKey, apiSecret, body, apiKeyKey);
  26. string authBase = Convert.ToBase64String(CHARSET_UTF8.GetBytes(authorizationData.Authorization));
  27. authBase = string.Format("{0}?{1}={2}&host={3}&date={4}", url, AUTH_KEY.ToLower(), UrlEncode(authBase, Encoding.UTF8),
  28. UrlEncode(authorizationData.Host, Encoding.UTF8), UrlEncode(authorizationData.Date, Encoding.UTF8));
  29. return authBase;
  30. }
  31. private static AuthorizationData assemble(string httpMethod, string requestUrl, string apiKey, string apiSecret, string body, string apiKeyKey)
  32. {
  33. if (string.IsNullOrEmpty(requestUrl))
  34. {
  35. throw new Exception("requestUr is empty.");
  36. }
  37. if (string.IsNullOrEmpty(apiKey))
  38. {
  39. throw new Exception("apiKey is empty.");
  40. }
  41. if (string.IsNullOrEmpty(apiSecret))
  42. {
  43. throw new Exception("apiSecret is empty.");
  44. }
  45. string httpRequestUrl = requestUrl.Replace("ws://", "http://").Replace("wss://", "https://");
  46. Uri uri = new Uri(httpRequestUrl);
  47. string dateStr = DateTime.UtcNow.GetDateTimeFormats('r')[0].ToString();
  48. string sha = getSignature(uri.Host, dateStr, getRequestLine(httpMethod, uri.AbsolutePath), apiSecret);
  49. string digest = null;
  50. if (!string.IsNullOrEmpty(body))
  51. {
  52. digest = signBody(body);
  53. }
  54. apiKeyKey = string.IsNullOrEmpty(apiKeyKey) ? "hmac api_key" : apiKeyKey;
  55. string authorization = string.Format("{0}=\"{1}\", algorithm=\"hmac-sha256\", headers=\"host date request-line{2}\", signature=\"{3}\"", apiKeyKey, apiKey, string.IsNullOrEmpty(digest) ? "" : " digest", sha);
  56. AuthorizationData authorizationData = new AuthorizationData();
  57. authorizationData.Date = dateStr;
  58. authorizationData.Host = uri.Host;
  59. authorizationData.Authorization = authorization;
  60. authorizationData.Digest = digest;
  61. return authorizationData;
  62. }
  63. private static string getRequestLine(string method, string path)
  64. {
  65. return string.Format("{0} {1} HTTP/1.1", method.ToUpper(), path);
  66. }
  67. private static string signBody(String body)
  68. {
  69. if (string.IsNullOrEmpty(body))
  70. {
  71. throw new Exception("body is empty.");
  72. }
  73. return signBody(CHARSET_UTF8.GetBytes(body));
  74. }
  75. private static string signBody(byte[] body)
  76. {
  77. if (null == body || body.Length == 0)
  78. {
  79. throw new Exception("body is empty.");
  80. }
  81. var sha256 = new SHA256Managed();
  82. byte[] hashmessage = sha256.ComputeHash(body);
  83. return Convert.ToBase64String(hashmessage);
  84. }
  85. private static string getSignature(string host, string date, string requestLine, string apiSecret)
  86. {
  87. if (string.IsNullOrEmpty(host))
  88. {
  89. throw new Exception("host is empty.");
  90. }
  91. if (string.IsNullOrEmpty(date))
  92. {
  93. throw new Exception("date is empty.");
  94. }
  95. if (string.IsNullOrEmpty(requestLine))
  96. {
  97. throw new Exception("requestLine is empty.");
  98. }
  99. if (string.IsNullOrEmpty(apiSecret))
  100. {
  101. throw new Exception("apiSecret is empty.");
  102. }
  103. StringBuilder builder = new StringBuilder("host: ").Append(host).Append("\n").Append("date: ").Append(date).Append("\n").Append(requestLine);
  104. byte[] keyByte = CHARSET_UTF8.GetBytes(apiSecret);
  105. byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(builder.ToString());
  106. using (var hmacsha256 = new HMACSHA256(keyByte))
  107. {
  108. byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
  109. return Convert.ToBase64String(hashmessage);
  110. }
  111. }
  112. private static string UrlEncode(string str, Encoding encoding)
  113. {
  114. StringBuilder sb = new StringBuilder();
  115. byte[] byStr = encoding.GetBytes(str);
  116. for (int i = 0; i < byStr.Length; i++)
  117. {
  118. sb.Append(@"%" + Convert.ToString(byStr[i], 16));
  119. }
  120. return (sb.ToString());
  121. }
  122. class AuthorizationData
  123. {
  124. public string Date { get; set; }
  125. public string Host { get; set; }
  126. public string Authorization { get; set; }
  127. public string Digest { get; set; }
  128. public Dictionary<string, string> GetHeader()
  129. {
  130. Dictionary<string, string> headers = new Dictionary<string, string>();
  131. headers.Add("Host", Host);
  132. headers.Add("Date", Date);
  133. if (!string.IsNullOrEmpty(Digest))
  134. {
  135. headers.Add("Digest", string.Format("%s=%s", DIGEST_NAME, Digest));
  136. }
  137. headers.Add(AUTH_KEY, Authorization);
  138. return headers;
  139. }
  140. }
  141. }
  142. }