ph-utils 0.17.2 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ type RGBColorObject = {
2
+ r: number;
3
+ g: number;
4
+ b: number;
5
+ a?: number;
6
+ };
7
+ type HSVColorObject = {
8
+ h: number;
9
+ s: number;
10
+ v: number;
11
+ };
12
+ type ColorType = string | RGBColorObject | HSVColorObject;
13
+ /**
14
+ * 将输入的颜色值转换为RGB对象格式。
15
+ *
16
+ * @param color 可以是字符串, 也可以是一个 HSV 对象[一个包含 h、s、v 属性的对象]
17
+ * @returns 返回一个包含r、g、b和a(透明度)属性的RGB对象。
18
+ * @throws 如果输入的字符串不是有效的颜色表示,则抛出错误。
19
+ */
20
+ export declare function toRgb(color: ColorType): RGBColorObject;
21
+ /**
22
+ * 将颜色转换为HSV颜色模型。
23
+ *
24
+ * @param color - 字符串或者RGB对象
25
+ * @returns 返回一个包含h、s、v属性的对象,代表HSV颜色值,其中h是色相(取值范围0到360),s是饱和度(取值范围0到1),v是明度(取值范围0到1)。
26
+ */
27
+ export declare function toHsv(color: ColorType): HSVColorObject;
28
+ /**
29
+ * 将RGB颜色对象转换为十六进制颜色字符串。
30
+ * @param rgb - 包含红色(r), 绿色(g), 蓝色(b)成分的对象。
31
+ * @returns 返回一个表示RGB颜色的十六进制字符串,例如"#FF0000"。
32
+ */
33
+ export declare function rgbToHex(rgb: RGBColorObject): string;
34
+ /**
35
+ * 将颜色转换为 16 进制字符串
36
+ * @param color - 颜色, 可以 rgb(0,0,0),rgba(0,0,0,0)字符串, 也可以是 rgb、hsv对象
37
+ * @returns 返回颜色的十六进制字符串,例如"#FF0000"
38
+ */
39
+ export declare function toHex(color: ColorType): string;
40
+ /**
41
+ * 调整给定颜色深[darken]浅[lighten]
42
+ * @param color - 输入的颜色,可以是任意颜色表示方式
43
+ * @param level - 调整深浅级别, 可以是小数。默认: 1
44
+ * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
45
+ *
46
+ * #### 1. 颜色变浅
47
+ *
48
+ * ```
49
+ * adjust('#4998f4', 3, true)
50
+ * ```
51
+ *
52
+ * @returns 返回调整后颜色的十六进制字符串表示。
53
+ */
54
+ export declare function adjust(color: ColorType, level?: number, light?: boolean): string;
55
+ export {};
@@ -0,0 +1,294 @@
1
+ const hueStep = 2; // 色相阶梯
2
+ const saturationStep = 0.16; // 饱和度阶梯,浅色部分
3
+ const saturationStep2 = 0.05; // 饱和度阶梯,深色部分
4
+ const brightnessStep1 = 0.05; // 亮度阶梯,浅色部分
5
+ const brightnessStep2 = 0.15; // 亮度阶梯,深色部分
6
+ /* 解析: rgb(0,0,0), rgba(0,0,0,0.1) 格式的正则 */
7
+ const rgbRegex = /rgba?\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(?:\s*,\s*(\d*\.?\d+))?\s*\)/;
8
+ /* 匹配 16 进制颜色字符串的正则: #fff, #ffffff */
9
+ const hexRegex = /^#(?:([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})|([0-9A-Fa-f]{1})([0-9A-Fa-f]{1})([0-9A-Fa-f]{1}))$/;
10
+ function isRgb(color) {
11
+ return color.r != null && color.g != null && color.b != null;
12
+ }
13
+ function isHsv(color) {
14
+ return color.h != null && color.s != null && color.v != null;
15
+ }
16
+ function parseRgbString(color) {
17
+ let colorValue = color.match(rgbRegex);
18
+ if (colorValue != null) {
19
+ const r = parseInt(colorValue[1].trim());
20
+ const g = parseInt(colorValue[2].trim());
21
+ const b = parseInt(colorValue[3].trim());
22
+ // 检查RGB值是否为NaN
23
+ if (isNaN(r) || isNaN(g) || isNaN(b)) {
24
+ throw new Error("Invalid RGB color.");
25
+ }
26
+ let a = 1;
27
+ // 如果是RGBA,解析透明度值
28
+ if (colorValue[4] != null) {
29
+ a = parseFloat(colorValue[4].trim());
30
+ if (isNaN(a)) {
31
+ a = 1;
32
+ }
33
+ }
34
+ return {
35
+ r,
36
+ g,
37
+ b,
38
+ a,
39
+ };
40
+ }
41
+ }
42
+ function parseHexString(color) {
43
+ const colorMatch = color.match(hexRegex);
44
+ if (colorMatch != null) {
45
+ // 判断是否是缩写形式的颜色(3个字符)
46
+ const isShortForm = colorMatch[4] !== undefined;
47
+ const r = parseInt(isShortForm ? colorMatch[4] + colorMatch[4] : colorMatch[1], 16);
48
+ const g = parseInt(isShortForm ? colorMatch[5] + colorMatch[5] : colorMatch[2], 16);
49
+ const b = parseInt(isShortForm ? colorMatch[6] + colorMatch[6] : colorMatch[3], 16);
50
+ return {
51
+ r,
52
+ g,
53
+ b,
54
+ };
55
+ }
56
+ }
57
+ /**
58
+ * 将输入的颜色值转换为RGB对象格式。
59
+ *
60
+ * @param color 可以是字符串, 也可以是一个 HSV 对象[一个包含 h、s、v 属性的对象]
61
+ * @returns 返回一个包含r、g、b和a(透明度)属性的RGB对象。
62
+ * @throws 如果输入的字符串不是有效的颜色表示,则抛出错误。
63
+ */
64
+ export function toRgb(color) {
65
+ if (typeof color === "string") {
66
+ let rgb = parseHexString(color);
67
+ if (rgb == null) {
68
+ rgb = parseRgbString(color);
69
+ }
70
+ if (rgb != null) {
71
+ return rgb;
72
+ }
73
+ throw new Error("Invalid color string");
74
+ }
75
+ else {
76
+ // 处理RGB对象输入
77
+ if (isRgb(color)) {
78
+ return color;
79
+ }
80
+ else if (isHsv(color)) {
81
+ return hsvToRgb(color);
82
+ }
83
+ throw new Error("Invalid color");
84
+ }
85
+ }
86
+ /**
87
+ * 将颜色转换为HSV颜色模型。
88
+ *
89
+ * @param color - 字符串或者RGB对象
90
+ * @returns 返回一个包含h、s、v属性的对象,代表HSV颜色值,其中h是色相(取值范围0到360),s是饱和度(取值范围0到1),v是明度(取值范围0到1)。
91
+ */
92
+ export function toHsv(color) {
93
+ if (isHsv(color))
94
+ return color;
95
+ const rgbColor = toRgb(color);
96
+ // 将RGB值标准化到0到1的范围
97
+ const r = rgbColor.r / 255;
98
+ const g = rgbColor.g / 255;
99
+ const b = rgbColor.b / 255;
100
+ // 获取最大和最小值,计算色彩的明度(V)
101
+ let max = Math.max(r, g, b);
102
+ let min = Math.min(r, g, b);
103
+ let delta = max - min;
104
+ let v = max;
105
+ // 计算饱和度(S)
106
+ let s = max === 0 ? 0 : delta / max;
107
+ // 计算色相(H)
108
+ let h = 0;
109
+ if (max === min) {
110
+ h = 0; // achromatic (no hue)
111
+ }
112
+ else {
113
+ // 根据最大值和最小值的不同,计算色相
114
+ switch (max) {
115
+ case r:
116
+ h = ((g - b) / delta + (g < b ? 6 : 0)) / 6;
117
+ break;
118
+ case g:
119
+ h = ((b - r) / delta + 2) / 6;
120
+ break;
121
+ case b:
122
+ h = ((r - g) / delta + 4) / 6;
123
+ break;
124
+ }
125
+ }
126
+ // 将色相值乘以360,转换为度数
127
+ h = h * 360;
128
+ return { h, s, v };
129
+ }
130
+ /**
131
+ * 将HSV颜色模型转换为RGB颜色模型。
132
+ * @param hsv 包含色相(h)、饱和度(s)和明度(v)的对象。s 和 v 可以是 0~1之间的小数, 也可以是 0~100 直接的整数, 推荐为小数,结果更精准
133
+ * @returns 返回一个包含红色(r)、绿色(g)和蓝色(b)值的对象,范围为0-255。
134
+ */
135
+ function hsvToRgb(hsv) {
136
+ // 将百分比形式的饱和度和明度转换为0-1的数值
137
+ let s = hsv.s;
138
+ let v = hsv.v;
139
+ if (s > 1) {
140
+ s /= 100;
141
+ }
142
+ if (v > 1) {
143
+ v /= 100;
144
+ }
145
+ const h = hsv.h;
146
+ // 根据色相值计算色相角,并计算出六个区域中的位置
147
+ var hi = Math.floor(h / 60) % 6;
148
+ var f = h / 60 - Math.floor(h / 60);
149
+ var p = v * (1 - s);
150
+ var q = v * (1 - f * s);
151
+ var t = v * (1 - (1 - f) * s);
152
+ // 根据色相角确定RGB值的具体计算
153
+ let r = 0, g = 0, b = 0;
154
+ switch (hi) {
155
+ case 0:
156
+ r = v;
157
+ g = t;
158
+ b = p;
159
+ break;
160
+ case 1:
161
+ r = q;
162
+ g = v;
163
+ b = p;
164
+ break;
165
+ case 2:
166
+ r = p;
167
+ g = v;
168
+ b = t;
169
+ break;
170
+ case 3:
171
+ r = p;
172
+ g = q;
173
+ b = v;
174
+ break;
175
+ case 4:
176
+ r = t;
177
+ g = p;
178
+ b = v;
179
+ break;
180
+ case 5:
181
+ r = v;
182
+ g = p;
183
+ b = q;
184
+ break;
185
+ }
186
+ // 将计算得到的RGB值从0-1范围转换为0-255范围
187
+ r = Math.round(r * 255);
188
+ g = Math.round(g * 255);
189
+ b = Math.round(b * 255);
190
+ return { r, g, b };
191
+ }
192
+ function getHue(hsv, i, light) {
193
+ let hue;
194
+ // 根据色相不同,色相转向不同
195
+ if (Math.round(hsv.h) >= 60 && Math.round(hsv.h) <= 240) {
196
+ hue = light
197
+ ? Math.round(hsv.h) - hueStep * i
198
+ : Math.round(hsv.h) + hueStep * i;
199
+ }
200
+ else {
201
+ hue = light
202
+ ? Math.round(hsv.h) + hueStep * i
203
+ : Math.round(hsv.h) - hueStep * i;
204
+ }
205
+ if (hue < 0) {
206
+ hue += 360;
207
+ }
208
+ else if (hue >= 360) {
209
+ hue -= 360;
210
+ }
211
+ return hue;
212
+ }
213
+ function getSaturation(hsv, i, light) {
214
+ // grey color don't change saturation
215
+ if (hsv.h === 0 && hsv.s === 0) {
216
+ return hsv.s;
217
+ }
218
+ let saturation;
219
+ if (light) {
220
+ saturation = hsv.s - saturationStep * i;
221
+ }
222
+ else {
223
+ saturation = hsv.s + saturationStep2 * i;
224
+ }
225
+ // 边界值修正
226
+ if (saturation > 1) {
227
+ saturation = 1;
228
+ }
229
+ if (saturation < 0.06) {
230
+ saturation = 0.06;
231
+ }
232
+ return Number(saturation.toFixed(2));
233
+ }
234
+ function getValue(hsv, i, light) {
235
+ let value;
236
+ if (light) {
237
+ value = hsv.v + brightnessStep1 * i;
238
+ }
239
+ else {
240
+ value = hsv.v - brightnessStep2 * i;
241
+ }
242
+ if (value > 1) {
243
+ value = 1;
244
+ }
245
+ return Number(value.toFixed(2));
246
+ }
247
+ /**
248
+ * 将RGB颜色对象转换为十六进制颜色字符串。
249
+ * @param rgb - 包含红色(r), 绿色(g), 蓝色(b)成分的对象。
250
+ * @returns 返回一个表示RGB颜色的十六进制字符串,例如"#FF0000"。
251
+ */
252
+ export function rgbToHex(rgb) {
253
+ // 将一个数字转换为两位数的十六进制字符串
254
+ const toHex = (n) => n.toString(16).padStart(2, "0");
255
+ // 将RGB颜色值转换为十六进制字符串并大写化
256
+ return `#${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`.toUpperCase();
257
+ }
258
+ /**
259
+ * 将颜色转换为 16 进制字符串
260
+ * @param color - 颜色, 可以 rgb(0,0,0),rgba(0,0,0,0)字符串, 也可以是 rgb、hsv对象
261
+ * @returns 返回颜色的十六进制字符串,例如"#FF0000"
262
+ */
263
+ export function toHex(color) {
264
+ if (typeof color === "string") {
265
+ if (hexRegex.test(color)) {
266
+ return color;
267
+ }
268
+ }
269
+ return rgbToHex(toRgb(color));
270
+ }
271
+ /**
272
+ * 调整给定颜色深[darken]浅[lighten]
273
+ * @param color - 输入的颜色,可以是任意颜色表示方式
274
+ * @param level - 调整深浅级别, 可以是小数。默认: 1
275
+ * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
276
+ *
277
+ * #### 1. 颜色变浅
278
+ *
279
+ * ```
280
+ * adjust('#4998f4', 3, true)
281
+ * ```
282
+ *
283
+ * @returns 返回调整后颜色的十六进制字符串表示。
284
+ */
285
+ export function adjust(color, level = 1, light = true) {
286
+ // 将输入的颜色转换为HSV格式
287
+ const hsv = toHsv(color);
288
+ // 根据level和light参数调整颜色的H、S、V值,并转换回十六进制颜色表示
289
+ return toHex({
290
+ h: getHue(hsv, level, light),
291
+ s: getSaturation(hsv, level, light),
292
+ v: getValue(hsv, level, light),
293
+ });
294
+ }
@@ -0,0 +1,33 @@
1
+ /** 配置信息 */
2
+ export declare let config: Record<string, any>;
3
+ /**
4
+ * 解析环境变量;
5
+ * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
6
+ * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
7
+ * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
8
+ *
9
+ * ```bash
10
+ * node test.js --NODE_ENV development
11
+ * // or
12
+ * node test.js -n development
13
+ * ```
14
+ *
15
+ * ```js
16
+ * // test.js
17
+ * parseEnvs();
18
+ * ```
19
+ *
20
+ * @returns
21
+ */
22
+ export declare function parseEnv(envFiles?: string[]): Record<string, string>;
23
+ /**
24
+ * 解析配置文件并合并内容。
25
+ *
26
+ * @param files - 要解析的配置文件列表,默认为 ["config.json", "config.local.json"]。
27
+ * @param runParseEnv - 是否运行环境变量解析,默认为 true。
28
+ * @returns 合并后的配置对象。
29
+ *
30
+ * 该函数会根据当前环境加载相应的配置文件,并将其内容合并到最终的配置对象中。
31
+ * 如果指定的文件列表中不包含环境特定的配置文件,则会自动添加。
32
+ */
33
+ export declare function parseConfig(files?: string[], runParseEnv?: boolean): Record<string, any>;
@@ -0,0 +1,99 @@
1
+ import { parseEnv as readEnv, parseArgs } from "node:util";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ /** 配置信息 */
5
+ export let config = {};
6
+ /**
7
+ * 解析环境变量;
8
+ * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
9
+ * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
10
+ * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
11
+ *
12
+ * ```bash
13
+ * node test.js --NODE_ENV development
14
+ * // or
15
+ * node test.js -n development
16
+ * ```
17
+ *
18
+ * ```js
19
+ * // test.js
20
+ * parseEnvs();
21
+ * ```
22
+ *
23
+ * @returns
24
+ */
25
+ export function parseEnv(envFiles = [".env", ".env.local"]) {
26
+ // development, test, production
27
+ const files = [...envFiles];
28
+ let nodeEnv = process.env.NODE_ENV;
29
+ const { values } = parseArgs({
30
+ options: {
31
+ NODE_ENV: {
32
+ type: "string",
33
+ short: "n",
34
+ },
35
+ },
36
+ strict: false,
37
+ });
38
+ if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
39
+ nodeEnv = values.NODE_ENV;
40
+ }
41
+ if (nodeEnv == null) {
42
+ nodeEnv = "production";
43
+ }
44
+ process.env.NODE_ENV = nodeEnv;
45
+ const envFile = `.env.${nodeEnv}`;
46
+ if (!files.includes(envFile)) {
47
+ files.push(envFile);
48
+ }
49
+ let envParsed = {};
50
+ for (let i = 0, len = files.length; i < len; i++) {
51
+ const file = join(process.cwd(), files[i]);
52
+ try {
53
+ const envContent = readFileSync(file, {
54
+ encoding: "utf-8",
55
+ });
56
+ const envValue = readEnv(envContent);
57
+ for (const key in envValue) {
58
+ process.env[key] = envValue[key];
59
+ envParsed[key] = envValue[key];
60
+ }
61
+ // eslint-disable-next-line
62
+ }
63
+ catch (err) { }
64
+ }
65
+ return envParsed;
66
+ }
67
+ /**
68
+ * 解析配置文件并合并内容。
69
+ *
70
+ * @param files - 要解析的配置文件列表,默认为 ["config.json", "config.local.json"]。
71
+ * @param runParseEnv - 是否运行环境变量解析,默认为 true。
72
+ * @returns 合并后的配置对象。
73
+ *
74
+ * 该函数会根据当前环境加载相应的配置文件,并将其内容合并到最终的配置对象中。
75
+ * 如果指定的文件列表中不包含环境特定的配置文件,则会自动添加。
76
+ */
77
+ export function parseConfig(files, runParseEnv = true) {
78
+ let d = [...(files || ["config.json", "config.local.json"])];
79
+ if (runParseEnv) {
80
+ parseEnv();
81
+ }
82
+ const envConfigPath = `config.${process.env.NODE_ENV}.json`;
83
+ if (!d.includes(envConfigPath)) {
84
+ d.push(envConfigPath);
85
+ }
86
+ for (let i = 0, len = d.length; i < len; i++) {
87
+ const filePath = join(process.cwd(), d[i]);
88
+ try {
89
+ const content = readFileSync(filePath, {
90
+ encoding: "utf-8",
91
+ });
92
+ let contentJson = JSON.parse(content);
93
+ config = { ...config, ...contentJson };
94
+ // eslint-disable-next-line
95
+ }
96
+ catch (err) { }
97
+ }
98
+ return config;
99
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 复制数据, 可以从多种类型的数据
3
+ * 1. 直接复制文本: await copy("待复制的文本")
4
+ * 2. 复制节点上的 data-copy-text:
5
+ * <button data-copy-text="这是待复制的文本">复制</button>
6
+ * await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
7
+ * 3. 直接复制节点本身数据: await copy('#a')
8
+ * @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
9
+ * @returns {Promise<boolean>} 是否复制成功
10
+ */
11
+ export declare function copy(source: string | HTMLElement): Promise<boolean>;
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 创建一个临时节点缓存待复制数据
3
+ * @param {String} value
4
+ * @return {HTMLElement}
5
+ */
6
+ function createFakeElement(value) {
7
+ const fakeElement = document.createElement("textarea");
8
+ fakeElement.style.border = "0";
9
+ fakeElement.style.padding = "0";
10
+ fakeElement.style.margin = "0";
11
+ fakeElement.style.position = "absolute";
12
+ fakeElement.style.left = "-9999px";
13
+ fakeElement.style.top = "-9999";
14
+ fakeElement.setAttribute("readonly", "");
15
+ fakeElement.value = value;
16
+ return fakeElement;
17
+ }
18
+ /** 通过执行 execCommand 来执行复制 */
19
+ function copyFromCommand(text) {
20
+ // 添加节点
21
+ const fakeEl = createFakeElement(text);
22
+ document.body.append(fakeEl);
23
+ fakeEl.focus();
24
+ fakeEl.select();
25
+ // 执行复制
26
+ const res = document.execCommand("copy");
27
+ fakeEl.remove(); // 删除节点
28
+ return Promise.resolve(res);
29
+ }
30
+ /** 使用 navigator.clipboard 复制 */
31
+ function copyFromClipboard(text) {
32
+ const theClipboard = navigator.clipboard;
33
+ if (theClipboard != null) {
34
+ return theClipboard
35
+ .writeText(text)
36
+ .then(() => {
37
+ Promise.resolve(true);
38
+ })
39
+ .catch(() => Promise.resolve(false));
40
+ }
41
+ return Promise.resolve(false);
42
+ }
43
+ /** 解析待复制的文本 */
44
+ function parseCopyText(source) {
45
+ let copyText = null; // 待复制文本
46
+ let sourceEl = null;
47
+ // 获取待复制数据
48
+ if (typeof source === "string") {
49
+ // 从节点拿数据
50
+ if (source.startsWith("#") || source.startsWith(".")) {
51
+ sourceEl = document.querySelector(source);
52
+ if (sourceEl == null) {
53
+ copyText = source;
54
+ }
55
+ }
56
+ else {
57
+ copyText = source;
58
+ }
59
+ }
60
+ if (source instanceof HTMLElement) {
61
+ sourceEl = source;
62
+ }
63
+ // 从节点获取待复制数据
64
+ if (sourceEl != null) {
65
+ if (sourceEl.hasAttribute("data-copy-text")) {
66
+ copyText = sourceEl.getAttribute("data-copy-text");
67
+ }
68
+ else {
69
+ const tagName = sourceEl.tagName;
70
+ if (tagName === "INPUT" || tagName === "TEXTAREA") {
71
+ copyText = sourceEl.value;
72
+ }
73
+ else {
74
+ copyText = sourceEl.textContent;
75
+ }
76
+ }
77
+ }
78
+ return copyText;
79
+ }
80
+ /**
81
+ * 复制数据, 可以从多种类型的数据
82
+ * 1. 直接复制文本: await copy("待复制的文本")
83
+ * 2. 复制节点上的 data-copy-text:
84
+ * <button data-copy-text="这是待复制的文本">复制</button>
85
+ * await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
86
+ * 3. 直接复制节点本身数据: await copy('#a')
87
+ * @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
88
+ * @returns {Promise<boolean>} 是否复制成功
89
+ */
90
+ export async function copy(source) {
91
+ // 待复制文本
92
+ const copyText = parseCopyText(source);
93
+ if (copyText == null) {
94
+ return Promise.resolve(false);
95
+ }
96
+ const v = await copyFromClipboard(copyText);
97
+ if (v === false) {
98
+ return copyFromCommand(copyText);
99
+ }
100
+ return Promise.resolve(true);
101
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * 将原始的二进制数据转换为 Hex String
3
+ * @param bf 待转换的原始数据
4
+ * @param upper 是否需要转换为大写
5
+ * @returns
6
+ */
7
+ export declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolean): string;
8
+ /**
9
+ * SHA 哈希算法
10
+ * @param message 待进行 hash 的数据
11
+ * @param upper 是否转换为大写, 默认为: false
12
+ * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
13
+ * @returns
14
+ */
15
+ export declare function sha(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
16
+ /**
17
+ * 哈希算法
18
+ * @param message 待进行 hash 的数据
19
+ * @param upper 是否转换为大写, 默认为: false
20
+ * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
21
+ * @returns
22
+ */
23
+ export declare function hash(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
24
+ type HMACAlgorithm = "SHA-256" | "SHA-512";
25
+ /**
26
+ * 使用 HMAC 算法计算消息的哈希值
27
+ * @param message - 需要计算哈希的消息字符串
28
+ * @param secret - 用于生成 HMAC 的密钥
29
+ * @param algorithm - HMAC 使用的哈希算法,默认为 "SHA-256"
30
+ * @param upper - 是否将结果转换为大写,默认为 false
31
+ * @returns 返回十六进制格式的 HMAC 哈希值
32
+ */
33
+ export declare function hmacHash(message: string, secret: string, algorithm?: HMACAlgorithm, upper?: boolean): Promise<string>;
34
+ /** 返回结果类似 */
35
+ type AlgorithmResType = "hex" | "hexUpper" | "base64" | "raw";
36
+ /**
37
+ * AES 加密
38
+ * @param message 待加密的数据
39
+ * @param key 加解密密钥
40
+ * @param encode 加密后的数据转换的形式, hex - 转换为16进制字符串, hexUpper - 转换为16进制且大写, base64 - 转换为 base64 形式
41
+ * @param iv 加解密向量
42
+ * @returns [加密数据,向量]
43
+ */
44
+ export declare function aesEncrypt(message: string, key: string, encode?: AlgorithmResType, iv?: null | Uint8Array | string): Promise<{
45
+ ciphertext: string | ArrayBuffer;
46
+ iv: string;
47
+ key: string;
48
+ }>;
49
+ /**
50
+ * AES 解密
51
+ * @param message 加密后的数据
52
+ * @param key 解密密钥
53
+ * @param iv 向量
54
+ * @param encode 加密后数据的形式: hex | base64
55
+ * @returns
56
+ */
57
+ export declare function aesDecrypt(message: Uint8Array | string, key: string, iv: string, encode?: AlgorithmResType): Promise<string>;
58
+ /**
59
+ * RSA 加密
60
+ * @param key 公钥
61
+ * @param message 待加密数据
62
+ * @param encode 返回类型
63
+ * @returns
64
+ */
65
+ export declare function rsaEncrypt(message: string, publicKey: string, encode?: AlgorithmResType): Promise<string | ArrayBuffer>;
66
+ /**
67
+ * RSA 解密
68
+ * @param key 私钥, 根据私钥解密
69
+ * @param message 加密后的数据
70
+ * @param encode 加密后的数据形式
71
+ * @returns
72
+ */
73
+ export declare function rsaDecrypt(privateKey: string, message: Uint8Array | string, encode?: AlgorithmResType): Promise<string>;
74
+ export {};