tencent.ts 2.7 KB

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