ph-utils 0.14.1 → 0.15.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.
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-----";
@@ -1,4 +1,5 @@
1
- type HashAlgorithmName = "md5" | "sha1" | "sha256";
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: [公钥, 私钥]
@@ -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/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.14.1",
71
+ "version": "0.15.0",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",