🤔 add(ds): 添加 ds 算法

This commit is contained in:
BTMuli
2023-05-05 18:18:17 +08:00
parent 592fe02d14
commit 53242d6170
5 changed files with 97 additions and 15 deletions

View File

@@ -2,17 +2,21 @@
* @file core utils TGUtils.ts
* @description 应用用到的工具函数
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
* @since Alpha v0.2.0
*/
import { parseAnnoContent } from "./parseAnno";
import { getAnnoCard } from "./getAnnoCard";
import { getDS } from "./getDS";
const TGUtils = {
Anno: {
getCard: getAnnoCard,
parseContent: parseAnnoContent,
},
User: {
getDS,
},
};
export default TGUtils;

33
src/core/utils/getDS.ts Normal file
View File

@@ -0,0 +1,33 @@
/**
* @description ds 算法
* @since Alpha v0.2.0
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
// Node
import { stringify } from "querystring";
// Tauri.Genshin
import { md5 } from "./tools";
import TGConstant from "../constant/TGConstant";
/**
* @description 获取 ds
* @since Alpha v0.2.0
* @version 2.34.1
* @param {string} query 查询字符串
* @param {string} body 请求体
* @returns {string} ds
*/
export function getDS (query: string, body: string): string {
const params = {
salt: TGConstant.SALT.Other.X4,
t: Math.floor(Date.now() / 1000).toString(),
r: Math.floor(Math.random() * 900000 + 100000).toString(),
b: body,
q: query,
};
const md5Str = md5(stringify(params));
const ds = `${params.t},${params.r},${md5Str}`;
return ds;
}

View File

@@ -2,9 +2,11 @@
* @file core utils tools.ts
* @description 应用用到的工具函数
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
* @since Alpha v0.2.0
*/
import crypto from "crypto";
/**
* @description 转义正则表达式
* @since Alpha v0.1.2
@@ -23,3 +25,28 @@ export function decodeRegExp (data: string): string {
res = res.replace(/&amp;/g, "&");
return res;
}
/**
* @description 获取随机字符串
* @since Alpha v0.2.0
* @param {number} length 字符串长度
* @returns {string} 随机字符串
*/
export function getRandomString (length: number): string {
const str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let res = "";
for (let i = 0; i < length; i++) {
res += str[Math.floor(Math.random() * str.length)];
}
return res;
}
/**
* @description md5 加密
* @since Alpha v0.2.0
* @param {string} data 要加密的内容
* @returns {string} 加密后的内容
*/
export function md5 (data: string): string {
return crypto.createHash("md5").update(data).digest("hex");
}