| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- // 建议在项目中安装 crypto-js: npm install crypto-js
- // 如果是纯 UTS 环境且不依赖插件,可以使用内置的 crypto 接口(视具体平台而定)
- // import { hmac } from '@/uni_modules/uni-crypto'; // 假设使用 uni-crypto 插件
- // 或者使用通用的 CryptoJS 逻辑
- import CryptoJS from 'crypto-js';
- export class Token {
- /**
- * 组装 Token
- */
- static assembleToken(version: string, resourceName: string, expirationTime: string, signatureMethod: string, accessKey: string): string {
- const res = encodeURIComponent(resourceName);
- const signature = this.generatorSignature(version, resourceName, expirationTime, accessKey, signatureMethod);
- const sig = encodeURIComponent(signature);
- return `version=${version}&res=${res}&et=${expirationTime}&method=${signatureMethod}&sign=${sig}`;
- }
- /**
- * 生成签名
- */
- static generatorSignature(version: string, resourceName: string, expirationTime: string, accessKey: string, signatureMethod: string): string {
- // 构造待加密文本:et + "\n" + method + "\n" + res + "\n" + version
- const encryptText = `${expirationTime}\n${signatureMethod}\n${resourceName}\n${version}`;
-
- // 调用 HMAC 加密并转为 Base64
- return this.hmacEncrypt(encryptText, accessKey, signatureMethod);
- }
- /**
- * HMAC 加密实现 (基于 CryptoJS 示例)
- */
- static hmacEncrypt(data: string, key: string, signatureMethod: string): string {
- // 1. 处理 Key: Java 中使用了 Base64.getDecoder().decode(key)
- const keyWords = CryptoJS.enc.Base64.parse(key);
-
- // 2. 选择算法 (sha1, md5, sha256)
- let hash: any;
- const method = signatureMethod.toLowerCase();
-
- if (method === 'sha1') {
- hash = CryptoJS.HmacSHA1(data, keyWords);
- } else if (method === 'sha256') {
- hash = CryptoJS.HmacSHA256(data, keyWords);
- } else if (method === 'md5') {
- hash = CryptoJS.HmacMD5(data, keyWords);
- } else {
- throw new Error("Unsupported signature method: " + signatureMethod);
- }
- // 3. 返回 Base64 字符串
- return CryptoJS.enc.Base64.stringify(hash);
- }
- }
- /**
- * 测试运行
- */
- export function testToken() {
- const version = "2018-10-31";
- const resourceName = "products/123123";
- // 过期时间:当前时间戳(秒)+ 100天
- const expirationTime = (Math.floor(Date.now() / 1000) + 100 * 24 * 60 * 60).toString();
- const signatureMethod = "sha1";
- const accessKey = "KuF3NT/jUBJ62LNBB/A8XZA9CqS3Cu79B/ABmfA1UCw=";
- const token = Token.assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);
- console.log("Authorization: " + token);
- }
- /**
- * 获取token
- * 参数:
- * res string 资源信息
- * method string 加密方式
- * key stirng 产品秘钥
- * version string 协议版本号
- * 返回:
- * return string 实际的token
- */
- export function getToken(res:string, method:string, key:string, version:string = "2018-10-31", ) {
- const resourceName = res;
- // 过期时间:当前时间戳(秒)+ 100天
- const expirationTime = (Math.floor(Date.now() / 1000) + 100 * 24 * 60 * 60).toString();
- const signatureMethod = method;
- const accessKey = key;
- const token = Token.assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);
- return token;
- }
|