befly-shared 1.2.0 → 1.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly-shared",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -36,5 +36,5 @@
36
36
  "merge-anything": "^6.0.6",
37
37
  "pathe": "^2.0.3"
38
38
  },
39
- "gitHead": "ebb51601e566ac01ef09824309a6ec60a5d32388"
39
+ "gitHead": "762170d80f1649d216fcf702752b1375d335ccc0"
40
40
  }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * 密码哈希工具
3
+ * 使用 SHA-256 + 盐值对密码进行单向哈希
4
+ */
5
+
6
+ /**
7
+ * 使用 SHA-256 对密码进行哈希
8
+ * @param password - 原始密码
9
+ * @param salt - 盐值,默认为 befly
10
+ * @returns 哈希后的密码(十六进制字符串)
11
+ */
12
+ export async function hashPassword(password: string, salt: string = 'befly'): Promise<string> {
13
+ const data = password + salt;
14
+
15
+ // 将字符串转换为 Uint8Array
16
+ const encoder = new TextEncoder();
17
+ const dataBuffer = encoder.encode(data);
18
+
19
+ // 使用 Web Crypto API 进行 SHA-256 哈希
20
+ const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
21
+
22
+ // 将 ArrayBuffer 转换为十六进制字符串
23
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
24
+ const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
25
+
26
+ return hashHex;
27
+ }