ph-utils 0.14.1 → 0.15.2
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.
- package/lib/crypto.d.ts +18 -0
- package/lib/crypto.js +24 -0
- package/lib/crypto_node.d.ts +18 -1
- package/lib/crypto_node.js +22 -1
- package/lib/dom.d.ts +3 -3
- package/lib/dom.js +35 -17
- package/package.json +1 -1
package/lib/crypto.d.ts
CHANGED
@@ -13,6 +13,24 @@ export declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolea
|
|
13
13
|
* @returns
|
14
14
|
*/
|
15
15
|
export declare function sha(message: string, 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, 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>;
|
16
34
|
/** 返回结果类似 */
|
17
35
|
type AlgorithmResType = "hex" | "hexUpper" | "base64" | "raw";
|
18
36
|
/**
|
package/lib/crypto.js
CHANGED
@@ -63,6 +63,30 @@ export async function sha(message, upper = false, algorithm = "SHA-256") {
|
|
63
63
|
const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgUint8);
|
64
64
|
return bufferToHex(hashBuffer, upper);
|
65
65
|
}
|
66
|
+
/**
|
67
|
+
* 哈希算法
|
68
|
+
* @param message 待进行 hash 的数据
|
69
|
+
* @param upper 是否转换为大写, 默认为: false
|
70
|
+
* @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
|
71
|
+
* @returns
|
72
|
+
*/
|
73
|
+
export async function hash(message, upper = false, algorithm = "SHA-256") {
|
74
|
+
return sha(message, upper, algorithm);
|
75
|
+
}
|
76
|
+
/**
|
77
|
+
* 使用 HMAC 算法计算消息的哈希值
|
78
|
+
* @param message - 需要计算哈希的消息字符串
|
79
|
+
* @param secret - 用于生成 HMAC 的密钥
|
80
|
+
* @param algorithm - HMAC 使用的哈希算法,默认为 "SHA-256"
|
81
|
+
* @param upper - 是否将结果转换为大写,默认为 false
|
82
|
+
* @returns 返回十六进制格式的 HMAC 哈希值
|
83
|
+
*/
|
84
|
+
export async function hmacHash(message, secret, algorithm = "SHA-256", upper = false) {
|
85
|
+
const encoder = new TextEncoder();
|
86
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: { name: algorithm } }, false, ["sign"]);
|
87
|
+
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(message));
|
88
|
+
return bufferToHex(signature, upper);
|
89
|
+
}
|
66
90
|
function parseRsaKey(pem) {
|
67
91
|
const pemHeader = "-----BEGIN PUBLIC KEY-----";
|
68
92
|
const pemFooter = "-----END PUBLIC KEY-----";
|
package/lib/crypto_node.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
type HashAlgorithmName = "md5" |
|
1
|
+
type HashAlgorithmName = "md5" | SHAHashAlgorithmName;
|
2
|
+
type SHAHashAlgorithmName = "sha1" | "sha256" | "sha512";
|
2
3
|
/**
|
3
4
|
* 进行 md5|sha1|sha256 数据摘要签名
|
4
5
|
* @param d 待加密的数据
|
@@ -6,6 +7,22 @@ type HashAlgorithmName = "md5" | "sha1" | "sha256";
|
|
6
7
|
* @param upper 返回的结果是否需要大写. Defaults to False.
|
7
8
|
*/
|
8
9
|
export declare function hashDigest(d: string, algorithm?: HashAlgorithmName, upper?: boolean): string;
|
10
|
+
/**
|
11
|
+
* 进行 md5|sha1|sha256 数据摘要签名
|
12
|
+
* @param d 待加密的数据
|
13
|
+
* @param algorithm 签名算法, md5、sha1、sha256. Defaults to "sha256".
|
14
|
+
* @param upper 返回的结果是否需要大写. Defaults to False.
|
15
|
+
*/
|
16
|
+
export declare function hash(d: string, algorithm?: HashAlgorithmName, upper?: boolean): string;
|
17
|
+
/**
|
18
|
+
* 使用 HMAC 算法对消息进行哈希处理
|
19
|
+
* @param message 待处理的消息
|
20
|
+
* @param key 用于 HMAC 算法的密钥
|
21
|
+
* @param algorithm 哈希算法,可选值为 "sha1"、"sha256" 或 "sha512",默认为 "sha256"
|
22
|
+
* @param upper 返回的结果是否需要大写,默认为 false
|
23
|
+
* @returns 经过 HMAC 哈希处理后的字符串
|
24
|
+
*/
|
25
|
+
export declare function hmacHash(message: string, key: string, algorithm?: SHAHashAlgorithmName, upper?: boolean): string;
|
9
26
|
/**
|
10
27
|
* 生成 RSA 签名密钥对
|
11
28
|
* @returns: [公钥, 私钥]
|
package/lib/crypto_node.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { createHash, createCipheriv, randomBytes, generateKeyPair, createDecipheriv, privateDecrypt, publicEncrypt, } from "node:crypto";
|
1
|
+
import { createHash, createCipheriv, randomBytes, generateKeyPair, createDecipheriv, privateDecrypt, publicEncrypt, createHmac, } from "node:crypto";
|
2
2
|
/**
|
3
3
|
* 进行 md5|sha1|sha256 数据摘要签名
|
4
4
|
* @param d 待加密的数据
|
@@ -9,6 +9,27 @@ export function hashDigest(d, algorithm = "sha256", upper = false) {
|
|
9
9
|
const hashed = createHash(algorithm).update(d).digest("hex");
|
10
10
|
return upper ? hashed.toUpperCase() : hashed;
|
11
11
|
}
|
12
|
+
/**
|
13
|
+
* 进行 md5|sha1|sha256 数据摘要签名
|
14
|
+
* @param d 待加密的数据
|
15
|
+
* @param algorithm 签名算法, md5、sha1、sha256. Defaults to "sha256".
|
16
|
+
* @param upper 返回的结果是否需要大写. Defaults to False.
|
17
|
+
*/
|
18
|
+
export function hash(d, algorithm = "sha256", upper = false) {
|
19
|
+
return hashDigest(d, algorithm, upper);
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* 使用 HMAC 算法对消息进行哈希处理
|
23
|
+
* @param message 待处理的消息
|
24
|
+
* @param key 用于 HMAC 算法的密钥
|
25
|
+
* @param algorithm 哈希算法,可选值为 "sha1"、"sha256" 或 "sha512",默认为 "sha256"
|
26
|
+
* @param upper 返回的结果是否需要大写,默认为 false
|
27
|
+
* @returns 经过 HMAC 哈希处理后的字符串
|
28
|
+
*/
|
29
|
+
export function hmacHash(message, key, algorithm = "sha256", upper = false) {
|
30
|
+
const hashed = createHmac(algorithm, key).update(message).digest("hex");
|
31
|
+
return upper ? hashed.toUpperCase() : hashed;
|
32
|
+
}
|
12
33
|
/**
|
13
34
|
* 生成 RSA 签名密钥对
|
14
35
|
* @returns: [公钥, 私钥]
|
package/lib/dom.d.ts
CHANGED
@@ -82,7 +82,7 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
|
82
82
|
* @param {function} event 事件处理函数
|
83
83
|
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
84
84
|
*/
|
85
|
-
export declare function on(element: HTMLElement | ShadowRoot | Document, listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
|
85
|
+
export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
|
86
86
|
eventFlag?: string;
|
87
87
|
})): void;
|
88
88
|
/**
|
@@ -91,7 +91,7 @@ export declare function on(element: HTMLElement | ShadowRoot | Document, listene
|
|
91
91
|
* @param listener - 事件名称。
|
92
92
|
* @param fn - 要移除的事件监听器函数。
|
93
93
|
*/
|
94
|
-
export declare function off(el: HTMLElement | ShadowRoot | Document, listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
|
94
|
+
export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
|
95
95
|
/**
|
96
96
|
* 判断事件是否应该继续传递。
|
97
97
|
* 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
|
@@ -124,7 +124,7 @@ export declare function text(element: HTMLElement, textstr?: string): string | n
|
|
124
124
|
* @param elems
|
125
125
|
* @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
|
126
126
|
*/
|
127
|
-
export declare function iterate(elems: NodeList |
|
127
|
+
export declare function iterate<T>(elems: NodeList | HTMLCollection | T[], fn: (el: HTMLElement | T, index: number) => any): void;
|
128
128
|
/**
|
129
129
|
* 设置或获取节点 data-* 属性
|
130
130
|
* @param elem
|
package/lib/dom.js
CHANGED
@@ -133,7 +133,14 @@ export function on(element, listener, fn, option) {
|
|
133
133
|
element.setAttribute(option.eventFlag, "__stop__");
|
134
134
|
}
|
135
135
|
}
|
136
|
-
element.
|
136
|
+
if (element.length) {
|
137
|
+
iterate(element, (elem) => {
|
138
|
+
elem.addEventListener(listener, fn, option);
|
139
|
+
});
|
140
|
+
}
|
141
|
+
else if (element) {
|
142
|
+
element.addEventListener(listener, fn, option);
|
143
|
+
}
|
137
144
|
}
|
138
145
|
/**
|
139
146
|
* 移除指定元素的事件监听器。
|
@@ -142,7 +149,14 @@ export function on(element, listener, fn, option) {
|
|
142
149
|
* @param fn - 要移除的事件监听器函数。
|
143
150
|
*/
|
144
151
|
export function off(el, listener, fn, option) {
|
145
|
-
el.
|
152
|
+
if (el.length) {
|
153
|
+
iterate(el, (elem) => {
|
154
|
+
elem.removeEventListener(listener, fn, option);
|
155
|
+
});
|
156
|
+
}
|
157
|
+
else if (el) {
|
158
|
+
el.removeEventListener(listener, fn, option);
|
159
|
+
}
|
146
160
|
}
|
147
161
|
/**
|
148
162
|
* 判断事件是否应该继续传递。
|
@@ -439,6 +453,8 @@ function toggleCssProperty(el, properties, method = "set") {
|
|
439
453
|
export function transition(el, nameOrProperties, dir = "enter", finish) {
|
440
454
|
const p = dir === "enter" ? "from" : "to";
|
441
455
|
let nameClass = "", activeClass = "";
|
456
|
+
/** 动画状态, -1 - 准备, 0 - 进行中, 1 - 完成 */
|
457
|
+
let status = -1;
|
442
458
|
const trans = [];
|
443
459
|
if (typeof nameOrProperties === "string") {
|
444
460
|
nameClass = `${nameOrProperties}-${dir}-${p}`;
|
@@ -452,6 +468,7 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
452
468
|
}
|
453
469
|
}
|
454
470
|
}
|
471
|
+
status = 0;
|
455
472
|
if (dir === "enter") {
|
456
473
|
if (nameClass) {
|
457
474
|
el.classList.add(nameClass);
|
@@ -490,25 +507,26 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
490
507
|
});
|
491
508
|
}
|
492
509
|
}
|
493
|
-
el.addEventListener("transitionend", () => {
|
494
|
-
if (
|
495
|
-
|
496
|
-
|
497
|
-
el.classList.remove(
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
if (trans) {
|
502
|
-
el.style.removeProperty("transition");
|
510
|
+
el.addEventListener("transitionend", (e) => {
|
511
|
+
if (status === 0) {
|
512
|
+
status = 1;
|
513
|
+
if (nameClass) {
|
514
|
+
el.classList.remove(activeClass);
|
515
|
+
requestAnimationFrame(() => {
|
516
|
+
el.classList.remove(nameClass);
|
517
|
+
});
|
503
518
|
}
|
504
|
-
|
519
|
+
else {
|
520
|
+
if (trans) {
|
521
|
+
el.style.removeProperty("transition");
|
522
|
+
}
|
505
523
|
requestAnimationFrame(() => {
|
506
524
|
toggleCssProperty(el, nameOrProperties, "remove");
|
507
525
|
});
|
508
|
-
}
|
509
|
-
|
510
|
-
|
511
|
-
|
526
|
+
}
|
527
|
+
if (finish) {
|
528
|
+
finish();
|
529
|
+
}
|
512
530
|
}
|
513
531
|
}, { once: true });
|
514
532
|
}
|