ph-utils 0.16.1 → 0.16.3
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 +2 -2
- package/lib/crypto.js +5 -2
- package/lib/crypto_node.d.ts +1 -1
- package/lib/crypto_node.js +23 -2
- package/lib/dom.d.ts +3 -2
- package/lib/dom.js +1 -1
- package/lib/web.js +2 -1
- package/package.json +1 -1
package/lib/crypto.d.ts
CHANGED
@@ -12,7 +12,7 @@ export declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolea
|
|
12
12
|
* @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
|
13
13
|
* @returns
|
14
14
|
*/
|
15
|
-
export declare function sha(message: string, upper?: boolean, algorithm?: string): Promise<string>;
|
15
|
+
export declare function sha(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
|
16
16
|
/**
|
17
17
|
* 哈希算法
|
18
18
|
* @param message 待进行 hash 的数据
|
@@ -20,7 +20,7 @@ export declare function sha(message: string, upper?: boolean, algorithm?: string
|
|
20
20
|
* @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
|
21
21
|
* @returns
|
22
22
|
*/
|
23
|
-
export declare function hash(message: string, upper?: boolean, algorithm?: string): Promise<string>;
|
23
|
+
export declare function hash(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
|
24
24
|
type HMACAlgorithm = "SHA-256" | "SHA-512";
|
25
25
|
/**
|
26
26
|
* 使用 HMAC 算法计算消息的哈希值
|
package/lib/crypto.js
CHANGED
@@ -59,8 +59,11 @@ function base64ToBuffer(data) {
|
|
59
59
|
* @returns
|
60
60
|
*/
|
61
61
|
export async function sha(message, upper = false, algorithm = "SHA-256") {
|
62
|
-
|
63
|
-
|
62
|
+
let msgBuffer = message;
|
63
|
+
if (typeof message === "string") {
|
64
|
+
msgBuffer = new TextEncoder().encode(message);
|
65
|
+
}
|
66
|
+
const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgBuffer);
|
64
67
|
return bufferToHex(hashBuffer, upper);
|
65
68
|
}
|
66
69
|
/**
|
package/lib/crypto_node.d.ts
CHANGED
@@ -43,7 +43,7 @@ export declare function aesEncrypt(key: string, input: string, upper?: boolean):
|
|
43
43
|
* @param iv 向量
|
44
44
|
* @returns
|
45
45
|
*/
|
46
|
-
export declare function aesDecrypt(input: string, key: string, iv
|
46
|
+
export declare function aesDecrypt(input: string, key: string | Buffer, iv?: string | Buffer, algorithm?: string): string;
|
47
47
|
/**
|
48
48
|
* RSA 公钥加密
|
49
49
|
* @param input 待加密字符串
|
package/lib/crypto_node.js
CHANGED
@@ -73,6 +73,16 @@ export function aesEncrypt(key, input, upper = false) {
|
|
73
73
|
iv.toString("hex"),
|
74
74
|
];
|
75
75
|
}
|
76
|
+
function aesAlgorithm(key) {
|
77
|
+
if (key.startsWith("aes-"))
|
78
|
+
return key;
|
79
|
+
let prefix = "aes-";
|
80
|
+
// 如果 key 不是以数字开头,则加上数字,例如:128,256 等
|
81
|
+
if (!/^\d/.test(key)) {
|
82
|
+
prefix += "256-";
|
83
|
+
}
|
84
|
+
return `${prefix}${key}`;
|
85
|
+
}
|
76
86
|
/**
|
77
87
|
* AES 解密
|
78
88
|
* @param input 加密后的数据
|
@@ -80,8 +90,19 @@ export function aesEncrypt(key, input, upper = false) {
|
|
80
90
|
* @param iv 向量
|
81
91
|
* @returns
|
82
92
|
*/
|
83
|
-
export function aesDecrypt(input, key, iv) {
|
84
|
-
|
93
|
+
export function aesDecrypt(input, key, iv, algorithm = "aes-256-cbc") {
|
94
|
+
let ivBuffer = null;
|
95
|
+
if (iv && !Buffer.isBuffer(iv)) {
|
96
|
+
ivBuffer = Buffer.from(iv, "hex");
|
97
|
+
}
|
98
|
+
let keyBuffer;
|
99
|
+
if (Buffer.isBuffer(key)) {
|
100
|
+
keyBuffer = key;
|
101
|
+
}
|
102
|
+
else {
|
103
|
+
keyBuffer = Buffer.from(key, "hex");
|
104
|
+
}
|
105
|
+
const cipher = createDecipheriv(aesAlgorithm(algorithm), keyBuffer, ivBuffer);
|
85
106
|
let decryptedData = cipher.update(input, "hex", "utf-8");
|
86
107
|
decryptedData += cipher.final("utf-8");
|
87
108
|
return decryptedData;
|
package/lib/dom.d.ts
CHANGED
@@ -75,6 +75,7 @@ export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
|
|
75
75
|
* @param clazz - 要切换的类名。
|
76
76
|
*/
|
77
77
|
export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
78
|
+
type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
|
78
79
|
/**
|
79
80
|
* 为节点添加事件处理
|
80
81
|
* @param {HTMLElement} element 添加事件的节点
|
@@ -82,7 +83,7 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
|
82
83
|
* @param {function} event 事件处理函数
|
83
84
|
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
84
85
|
*/
|
85
|
-
export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn:
|
86
|
+
export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | (AddEventListenerOptions & {
|
86
87
|
eventFlag?: string;
|
87
88
|
})): void;
|
88
89
|
/**
|
@@ -91,7 +92,7 @@ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCo
|
|
91
92
|
* @param listener - 事件名称。
|
92
93
|
* @param fn - 要移除的事件监听器函数。
|
93
94
|
*/
|
94
|
-
export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn:
|
95
|
+
export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | EventListenerOptions): void;
|
95
96
|
/**
|
96
97
|
* 判断事件是否应该继续传递。
|
97
98
|
* 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
|
package/lib/dom.js
CHANGED
package/lib/web.js
CHANGED