token.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // 建议在项目中安装 crypto-js: npm install crypto-js
  2. // 如果是纯 UTS 环境且不依赖插件,可以使用内置的 crypto 接口(视具体平台而定)
  3. // import { hmac } from '@/uni_modules/uni-crypto'; // 假设使用 uni-crypto 插件
  4. // 或者使用通用的 CryptoJS 逻辑
  5. import CryptoJS from 'crypto-js';
  6. export class Token {
  7. /**
  8. * 组装 Token
  9. */
  10. static assembleToken(version: string, resourceName: string, expirationTime: string, signatureMethod: string, accessKey: string): string {
  11. const res = encodeURIComponent(resourceName);
  12. const signature = this.generatorSignature(version, resourceName, expirationTime, accessKey, signatureMethod);
  13. const sig = encodeURIComponent(signature);
  14. return `version=${version}&res=${res}&et=${expirationTime}&method=${signatureMethod}&sign=${sig}`;
  15. }
  16. /**
  17. * 生成签名
  18. */
  19. static generatorSignature(version: string, resourceName: string, expirationTime: string, accessKey: string, signatureMethod: string): string {
  20. // 构造待加密文本:et + "\n" + method + "\n" + res + "\n" + version
  21. const encryptText = `${expirationTime}\n${signatureMethod}\n${resourceName}\n${version}`;
  22. // 调用 HMAC 加密并转为 Base64
  23. return this.hmacEncrypt(encryptText, accessKey, signatureMethod);
  24. }
  25. /**
  26. * HMAC 加密实现 (基于 CryptoJS 示例)
  27. */
  28. static hmacEncrypt(data: string, key: string, signatureMethod: string): string {
  29. // 1. 处理 Key: Java 中使用了 Base64.getDecoder().decode(key)
  30. const keyWords = CryptoJS.enc.Base64.parse(key);
  31. // 2. 选择算法 (sha1, md5, sha256)
  32. let hash: any;
  33. const method = signatureMethod.toLowerCase();
  34. if (method === 'sha1') {
  35. hash = CryptoJS.HmacSHA1(data, keyWords);
  36. } else if (method === 'sha256') {
  37. hash = CryptoJS.HmacSHA256(data, keyWords);
  38. } else if (method === 'md5') {
  39. hash = CryptoJS.HmacMD5(data, keyWords);
  40. } else {
  41. throw new Error("Unsupported signature method: " + signatureMethod);
  42. }
  43. // 3. 返回 Base64 字符串
  44. return CryptoJS.enc.Base64.stringify(hash);
  45. }
  46. }
  47. /**
  48. * 测试运行
  49. */
  50. export function testToken() {
  51. const version = "2018-10-31";
  52. const resourceName = "products/123123";
  53. // 过期时间:当前时间戳(秒)+ 100天
  54. const expirationTime = (Math.floor(Date.now() / 1000) + 100 * 24 * 60 * 60).toString();
  55. const signatureMethod = "sha1";
  56. const accessKey = "KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw=";
  57. const token = Token.assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);
  58. console.log("Authorization: " + token);
  59. }
  60. /**
  61. * 获取token
  62. * 参数:
  63. * res string 资源信息
  64. * method string 加密方式
  65. * key stirng 产品秘钥
  66. * version string 协议版本号
  67. * 返回:
  68. * return string 实际的token
  69. */
  70. export function getToken(res:string, method:string, key:string, version:string = "2018-10-31", ) {
  71. const resourceName = res;
  72. // 过期时间:当前时间戳(秒)+ 100天
  73. const expirationTime = (Math.floor(Date.now() / 1000) + 100 * 24 * 60 * 60).toString();
  74. const signatureMethod = method;
  75. const accessKey = key;
  76. const token = Token.assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);
  77. return token;
  78. }