BytePlusAuthUtils.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. public class BytePlusAuthUtils
  4. {
  5. /**
  6. *
  7. * @param ak: AccessKey
  8. * @param sk: SecretKey
  9. * @param expirationSeconds: The expiration time. Units are in seconds
  10. * @param method: GET, POST, PUT
  11. * @param uriPath: The requestd path. Not a complete URL
  12. * @param params: The requested parameters
  13. * @param body: The requested json body
  14. * @return
  15. */
  16. public static string Sign(string ak, string sk, int expirationSeconds, string method, string uriPath, Dictionary<string, string> parameters, string body)
  17. {
  18. string cm = CanonicalMethod(method);
  19. string cu = CanonicalUrl(uriPath);
  20. string cp = CanonicalParam(parameters);
  21. string cb = CanonicalBody(body);
  22. string text = cm + "\n" + cu + "\n" + cp + "\n" + cb;
  23. return DoSign(ak, sk, expirationSeconds, text);
  24. }
  25. private static string CanonicalMethod(string method)
  26. {
  27. return "HTTPMethod:" + method;
  28. }
  29. private static string CanonicalUrl(string url)
  30. {
  31. return "CanonicalURI:" + url;
  32. }
  33. private static string CanonicalParam(Dictionary<string, string> parameters)
  34. {
  35. string res = "CanonicalQueryString:";
  36. if (parameters == null || parameters.Count == 0)
  37. {
  38. return res;
  39. }
  40. foreach (var key in parameters.Keys)
  41. {
  42. res += FormatKeyValue(key, parameters[key]) + "&";
  43. }
  44. return res.Substring(0, res.Length - 1);
  45. }
  46. private static string FormatKeyValue(string key, string value)
  47. {
  48. return key + "=" + value;
  49. }
  50. private static string CanonicalBody(string body)
  51. {
  52. string res = "CanonicalBody:";
  53. if (body == null)
  54. {
  55. return res;
  56. }
  57. else
  58. {
  59. return res + body;
  60. }
  61. }
  62. private static string DoSign(string ak, string sk, int expiration, string text)
  63. {
  64. string signKeyInfo = "ak-v1/" + ak + "/" + (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds + "/" + expiration;
  65. string signKey = Sha256Hmac(signKeyInfo, sk);
  66. string signResult = Sha256Hmac(text, signKey);
  67. return signKeyInfo + "/" + signResult;
  68. }
  69. private static string Sha256Hmac(string message, string secret)
  70. {
  71. string hash = "";
  72. using (var hmacsha256 = new HMACSHA256(Convert.FromBase64String(secret)))
  73. {
  74. var bytes = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(message));
  75. hash = ByteArrayToHexString(bytes);
  76. }
  77. return hash;
  78. }
  79. private static string ByteArrayToHexString(byte[] b)
  80. {
  81. StringBuilder hs = new StringBuilder();
  82. foreach (byte c in b)
  83. {
  84. hs.Append(c.ToString("x2"));
  85. }
  86. return hs.ToString().ToLower();
  87. }
  88. }