tencent.ts 2.9 KB

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