sign.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { SHA256, HmacSHA256, enc } from '../third/crypto';
  2. import SDK_VERSION from '../constant/version';
  3. export function sign(action, filePath) {
  4. return new Promise((resolve) => {
  5. const {
  6. getAuthorization,
  7. tmpSecretId,
  8. tmpSecretKey,
  9. secretId,
  10. secretKey,
  11. ocrOption,
  12. token,
  13. } = wx.clientInfo;
  14. const finalSecretId = getAuthorization ? tmpSecretId : secretId;
  15. const finalSecretKey = getAuthorization ? tmpSecretKey : secretKey;
  16. const host = 'ocr.tencentcloudapi.com';
  17. const service = 'ocr';
  18. const version = '2018-11-19';
  19. const timestamp = Math.round(Date.now() / 1000);
  20. const algorithm = 'TC3-HMAC-SHA256';
  21. // step 1: build canonical request string
  22. const httpRequestMethod = 'POST';
  23. const canonicalUri = '/';
  24. const canonicalQueryString = '';
  25. const canonicalHeaders = `content-type:application/json; charset=utf-8\nhost:${host}\n`;
  26. const signedHeaders = 'content-type;host';
  27. const temPayload = {
  28. ImageBase64: wx.getFileSystemManager().readFileSync(filePath, 'base64'),
  29. };
  30. if (ocrOption) {
  31. Object.keys(ocrOption).forEach((item) => {
  32. if (item === 'Config') {
  33. temPayload[item] = JSON.stringify(ocrOption[item]);
  34. } else {
  35. temPayload[item] = ocrOption[item];
  36. }
  37. });
  38. }
  39. const payload = JSON.stringify(temPayload);
  40. const hashedRequestPayload = SHA256(payload);
  41. const canonicalRequest = `${httpRequestMethod}\n${canonicalUri}\n${canonicalQueryString}\n${canonicalHeaders}\n${signedHeaders}\n${hashedRequestPayload}`;
  42. // step 2: build string to sign
  43. const t = new Date();
  44. const date = t.toISOString().substr(0, 10);
  45. const credentialScope = `${date}/${service}/tc3_request`;
  46. const hashedCanonicalRequest = SHA256(canonicalRequest);
  47. const stringToSign = `${algorithm}\n${timestamp}\n${credentialScope}\n${hashedCanonicalRequest}`;
  48. const secretDate = HmacSHA256(date, `TC3${finalSecretKey}`);
  49. const secretService = HmacSHA256(service, secretDate);
  50. const secretSigning = HmacSHA256('tc3_request', secretService);
  51. const signature = enc.Hex.stringify(HmacSHA256(stringToSign, secretSigning));
  52. const Algorithm = 'TC3-HMAC-SHA256';
  53. const SignedHeaders = 'content-type;host';
  54. const Authorization = `${Algorithm
  55. } `
  56. + `Credential=${
  57. finalSecretId
  58. }/${
  59. credentialScope
  60. }, `
  61. + `SignedHeaders=${
  62. SignedHeaders
  63. }, `
  64. + `Signature=${
  65. signature}`;
  66. const header = {
  67. Authorization,
  68. 'Content-Type': 'application/json; charset=UTF-8',
  69. 'X-TC-Action': action,
  70. 'X-TC-Timestamp': timestamp,
  71. 'X-TC-Version': version,
  72. 'X-TC-Region': 'ap-guangzhou',
  73. 'X-TC-RequestClient': `WXAPP_SDK_OcrSDK_${SDK_VERSION}`,
  74. };
  75. if (getAuthorization) {
  76. header['X-TC-Token'] = token;
  77. }
  78. wx.request({
  79. url: 'https://ocr.tencentcloudapi.com',
  80. data: payload,
  81. method: 'POST',
  82. header,
  83. success({ data: { Response } }) {
  84. console.log(Response);
  85. resolve(Response);
  86. },
  87. fail(err) {
  88. console.error('&&&&', err);
  89. resolve(err);
  90. },
  91. });
  92. });
  93. }