GenerateTestUserSig.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import LibGenerateTestUserSig from './lib-generate-test-usersig-es.min.js';
  2. /**
  3. * Signature expiration time, which should not be too short
  4. * Time unit: second
  5. * Default time: 7 * 24 * 60 * 60 = 604800 = 7days
  6. */
  7. const EXPIRETIME = 604800;
  8. /**
  9. * Module: GenerateTestUserSig
  10. *
  11. * Description: Generates UserSig for testing. UserSig is a security signature designed by Tencent Cloud for its cloud services.
  12. * It is calculated based on `SDKAppID`, `UserID`, and `EXPIRETIME` using the HMAC-SHA256 encryption algorithm.
  13. *
  14. * Attention: For the following reasons, do not use the code below in your commercial application.
  15. *
  16. * The code may be able to calculate UserSig correctly, but it is only for quick testing of the SDK’s basic features, not for commercial applications.
  17. * `SECRETKEY` in client code can be easily decompiled and reversed, especially on web.
  18. * Once your key is disclosed, attackers will be able to steal your Tencent Cloud traffic.
  19. *
  20. * The correct method is to deploy the `UserSig` calculation code and encryption key on your project server so that your application can request from your server a `UserSig` that is calculated whenever one is needed.
  21. * Given that it is more difficult to hack a server than a client application, server-end calculation can better protect your key.
  22. *
  23. * Reference: https://cloud.tencent.com/document/product/647/17275#Server
  24. */
  25. function genTestUserSig(options) {
  26. const { SDKAppID, secretKey, userID } = options;
  27. const generator = new LibGenerateTestUserSig(SDKAppID, secretKey, EXPIRETIME);
  28. const userSig = generator.genTestUserSig(userID);
  29. return {
  30. SDKAppID,
  31. userSig,
  32. };
  33. }
  34. export { genTestUserSig, EXPIRETIME };