tencent.ts 2.9 KB

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