tencent.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import hash from "hash.js";
  2. // 使用 SHA-256 和 secret 进行 HMAC 加密
  3. function sha256(message: any, secret = "", encoding?: string) {
  4. return hash
  5. .hmac(hash.sha256 as any, secret)
  6. .update(message)
  7. .digest(encoding as any);
  8. }
  9. // 使用 SHA-256 进行哈希
  10. function getHash(message: any, encoding = "hex") {
  11. return hash
  12. .sha256()
  13. .update(message)
  14. .digest(encoding as any);
  15. }
  16. function getDate(timestamp: number) {
  17. const date = new Date(timestamp * 1000);
  18. const year = date.getUTCFullYear();
  19. const month = ("0" + (date.getUTCMonth() + 1)).slice(-2);
  20. const day = ("0" + date.getUTCDate()).slice(-2);
  21. return `${year}-${month}-${day}`;
  22. }
  23. export async function getHeader(
  24. payload: any,
  25. SECRET_ID: string,
  26. SECRET_KEY: string,
  27. ) {
  28. // https://cloud.tencent.com/document/api/1729/105701
  29. const endpoint = "hunyuan.tencentcloudapi.com";
  30. const service = "hunyuan";
  31. const region = ""; // optional
  32. const action = "ChatCompletions";
  33. const version = "2023-09-01";
  34. const timestamp = Math.floor(Date.now() / 1000);
  35. //时间处理, 获取世界时间日期
  36. const date = getDate(timestamp);
  37. // ************* 步骤 1:拼接规范请求串 *************
  38. const hashedRequestPayload = getHash(payload);
  39. const httpRequestMethod = "POST";
  40. const contentType = "application/json";
  41. const canonicalUri = "/";
  42. const canonicalQueryString = "";
  43. const canonicalHeaders =
  44. `content-type:${contentType}\n` +
  45. "host:" +
  46. endpoint +
  47. "\n" +
  48. "x-tc-action:" +
  49. action.toLowerCase() +
  50. "\n";
  51. const signedHeaders = "content-type;host;x-tc-action";
  52. const canonicalRequest = [
  53. httpRequestMethod,
  54. canonicalUri,
  55. canonicalQueryString,
  56. canonicalHeaders,
  57. signedHeaders,
  58. hashedRequestPayload,
  59. ].join("\n");
  60. // ************* 步骤 2:拼接待签名字符串 *************
  61. const algorithm = "TC3-HMAC-SHA256";
  62. const hashedCanonicalRequest = getHash(canonicalRequest);
  63. const credentialScope = date + "/" + service + "/" + "tc3_request";
  64. const stringToSign =
  65. algorithm +
  66. "\n" +
  67. timestamp +
  68. "\n" +
  69. credentialScope +
  70. "\n" +
  71. hashedCanonicalRequest;
  72. // ************* 步骤 3:计算签名 *************
  73. const kDate = sha256(date, "TC3" + SECRET_KEY);
  74. const kService = sha256(service, kDate);
  75. const kSigning = sha256("tc3_request", kService);
  76. const signature = sha256(stringToSign, kSigning, "hex");
  77. // ************* 步骤 4:拼接 Authorization *************
  78. const authorization =
  79. algorithm +
  80. " " +
  81. "Credential=" +
  82. SECRET_ID +
  83. "/" +
  84. credentialScope +
  85. ", " +
  86. "SignedHeaders=" +
  87. signedHeaders +
  88. ", " +
  89. "Signature=" +
  90. signature;
  91. return {
  92. Authorization: authorization,
  93. "Content-Type": contentType,
  94. Host: endpoint,
  95. "X-TC-Action": action,
  96. "X-TC-Timestamp": timestamp.toString(),
  97. "X-TC-Version": version,
  98. "X-TC-Region": region,
  99. };
  100. }