DingTalkSignatureUtil.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Top.Api.Util
  6. {
  7. public class DingTalkSignatureUtil
  8. {
  9. private static string DEFAULT_ENCODING = "UTF-8";
  10. /* Signature method. */
  11. private static string ALGORITHM = "HmacSHA256";
  12. private static string NEW_LINE = "\n";
  13. // 获取签名所需要的字符串
  14. public static string GetCanonicalStringForIsv(long timestamp, string suiteTicket)
  15. {
  16. StringBuilder canonicalString = new StringBuilder();
  17. canonicalString.Append(timestamp);
  18. if (suiteTicket != null)
  19. {
  20. canonicalString.Append(NEW_LINE).Append(suiteTicket);
  21. }
  22. return canonicalString.ToString();
  23. }
  24. /**
  25. * 计算签名
  26. * @param canonicalString 签名
  27. * @param secret 签名秘钥
  28. * @return
  29. */
  30. public static string ComputeSignature(string secret, string canonicalString)
  31. {
  32. byte[] signData = Sign(Encoding.UTF8.GetBytes(canonicalString), Encoding.UTF8.GetBytes(secret));
  33. return Convert.ToBase64String(signData);
  34. }
  35. private static byte[] Sign(byte[] key, byte[] data)
  36. {
  37. HMACSHA256 sha256 = new HMACSHA256(data);
  38. return sha256.ComputeHash(key);
  39. }
  40. // 拼接url参数
  41. public static String ParamToQueryString(IDictionary<String, String> param, String charset)
  42. {
  43. if (param == null || param.Count == 0) {
  44. return null;
  45. }
  46. StringBuilder paramString = new StringBuilder();
  47. bool first = true;
  48. foreach (string key in param.Keys)
  49. {
  50. string value = param[key];
  51. if (!first)
  52. {
  53. paramString.Append("&");
  54. }
  55. // Urlencode each request parameter
  56. paramString.Append(UrlEncode(key, charset));
  57. if (value != null)
  58. {
  59. paramString.Append("=").Append(DingTalkSignatureUtil.UrlEncode(value, charset));
  60. }
  61. first = false;
  62. }
  63. return paramString.ToString();
  64. }
  65. /**
  66. * Encode a URL segment with special chars replaced.
  67. */
  68. public static String UrlEncode(String value, String encoding)
  69. {
  70. if (value == null)
  71. {
  72. return "";
  73. }
  74. String encoded = System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.UTF8);
  75. return encoded.Replace("+", "%20").Replace("*", "%2A")
  76. .Replace("~", "%7E").Replace("/", "%2F");
  77. }
  78. /**
  79. * 生成随机数
  80. * @return
  81. */
  82. public static String GetRandomStr(int count)
  83. {
  84. String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  85. Random random = new Random();
  86. StringBuilder sb = new StringBuilder();
  87. for (int i = 0; i < count; i++)
  88. {
  89. int number = random.Next(value.Length);
  90. sb.Append(CharAt(value, number));
  91. }
  92. return sb.ToString();
  93. }
  94. public static string CharAt(string value, int index)
  95. {
  96. if (index >= value.Length || index < 0)
  97. {
  98. return "";
  99. }
  100. return value.Substring(index, 1);
  101. }
  102. }
  103. }