ddan-js 2.6.2 → 2.6.4

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,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const convert_1 = require("./convert");
4
+ const generateKeys = async () => {
5
+ try {
6
+ // 生成RSA密钥对
7
+ const keyPair = await window.crypto.subtle.generateKey({
8
+ name: 'RSA-OAEP',
9
+ modulusLength: 2048,
10
+ publicExponent: new Uint8Array([1, 0, 1]),
11
+ hash: 'SHA-256',
12
+ }, true, // 是否可导出密钥
13
+ ['encrypt', 'decrypt']);
14
+ // 导出公钥为SPKI格式(可共享)
15
+ const publicBuffer = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
16
+ const publicKey = format_pem_content(publicBuffer);
17
+ const publicKeyPem = format_pem_key(publicKey, 'PUBLIC KEY');
18
+ // 导出私钥为PKCS#8格式(需保护)
19
+ const privateBuffer = await window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
20
+ const privateKey = format_pem_content(privateBuffer);
21
+ const privateKeyPem = format_pem_key(privateKey, 'PRIVATE KEY');
22
+ return {
23
+ publicKey,
24
+ publicKeyPem,
25
+ privateKey,
26
+ privateKeyPem,
27
+ };
28
+ }
29
+ catch (err) {
30
+ console.error('generateKeys failed:', err);
31
+ }
32
+ };
33
+ const format_pem_content = (buffer) => {
34
+ try {
35
+ const base64String = convert_1.default.ab2str(buffer, true);
36
+ return base64String.match(/.{1,64}/g)?.join('\n');
37
+ }
38
+ catch (error) {
39
+ return '';
40
+ }
41
+ };
42
+ const format_pem_key = (pemContent = '', label = 'PUBLIC KEY') => {
43
+ return `-----BEGIN ${label}-----\n${pemContent}\n-----END ${label}-----`;
44
+ };
45
+ const encrypt = async (data, publicKey) => {
46
+ try {
47
+ const encodedData = new TextEncoder().encode(data);
48
+ const key = await window.crypto.subtle.importKey('spki', convert_1.default.str2ab(publicKey, true), {
49
+ name: 'RSA-OAEP',
50
+ hash: 'SHA-256',
51
+ }, true, ['encrypt']);
52
+ const encrypted = await window.crypto.subtle.encrypt({
53
+ name: 'RSA-OAEP',
54
+ }, key, encodedData);
55
+ return convert_1.default.ab2str(encrypted, true);
56
+ }
57
+ catch (err) {
58
+ console.error('encrypt failed:', err);
59
+ }
60
+ };
61
+ const decrypt = async (encryptedData, privateKey) => {
62
+ try {
63
+ const encrypted = convert_1.default.str2ab(encryptedData, true);
64
+ const key = await window.crypto.subtle.importKey('pkcs8', convert_1.default.stringToArrayBuffer(window.atob(privateKey)), {
65
+ name: 'RSA-OAEP',
66
+ hash: 'SHA-256',
67
+ }, true, ['decrypt']);
68
+ const decrypted = await window.crypto.subtle.decrypt({
69
+ name: 'RSA-OAEP',
70
+ }, key, encrypted);
71
+ const decodedData = new TextDecoder().decode(decrypted);
72
+ return decodedData;
73
+ }
74
+ catch (err) {
75
+ console.error('decrypt failed:', err);
76
+ }
77
+ };
78
+ exports.default = {
79
+ generateKeys,
80
+ format_pem_key,
81
+ format_pem_content,
82
+ encrypt,
83
+ decrypt
84
+ };
@@ -6,9 +6,10 @@ export default class DPersist implements Ddan.IPersist {
6
6
  fields: string[];
7
7
  constructor(config?: Ddan.IDPersistConfig);
8
8
  save: {
9
- (...debouncedArgs: any[]): any;
9
+ (...args: any[]): any;
10
10
  cancel: () => void;
11
11
  flush: () => any;
12
+ pending: () => boolean;
12
13
  };
13
14
  read(): any;
14
15
  handleSave(target: Ddan.IDStore, key: string): void;
@@ -58,6 +58,7 @@ declare const dUtil: {
58
58
  };
59
59
  uuid: (len?: number, radix?: number) => string;
60
60
  guid: (len: number, prefix?: boolean, sep?: string) => string;
61
+ getHexString: (len: number) => string;
61
62
  keyNumber: string;
62
63
  keyLower: string;
63
64
  keyUpper: string;
@@ -65,6 +66,10 @@ declare const dUtil: {
65
66
  uint8ArrayToString: (arr: any) => string;
66
67
  stringToArrayBuffer: (str: any) => ArrayBuffer;
67
68
  arrayBufferToString: (data: ArrayBuffer, base64?: boolean) => string;
69
+ strToHexString: (str: string) => string;
70
+ hexStringToStr: (hexstr: string) => string;
71
+ str2ab: (str?: string, base64?: boolean) => ArrayBuffer;
72
+ ab2str: (data: ArrayBuffer, base64?: boolean) => string;
68
73
  gbk: {
69
74
  gbkLength: (str: string) => number;
70
75
  gbkCut: (source: string, len: number) => string;
@@ -207,6 +212,7 @@ declare const dHook: {
207
212
  };
208
213
  uuid: (len?: number, radix?: number) => string;
209
214
  guid: (len: number, prefix?: boolean, sep?: string) => string;
215
+ getHexString: (len: number) => string;
210
216
  keyNumber: string;
211
217
  keyLower: string;
212
218
  keyUpper: string;
@@ -214,6 +220,10 @@ declare const dHook: {
214
220
  uint8ArrayToString: (arr: any) => string;
215
221
  stringToArrayBuffer: (str: any) => ArrayBuffer;
216
222
  arrayBufferToString: (data: ArrayBuffer, base64?: boolean) => string;
223
+ strToHexString: (str: string) => string;
224
+ hexStringToStr: (hexstr: string) => string;
225
+ str2ab: (str?: string, base64?: boolean) => ArrayBuffer;
226
+ ab2str: (data: ArrayBuffer, base64?: boolean) => string;
217
227
  };
218
228
  declare const dMini: {
219
229
  mini: {
@@ -442,6 +452,18 @@ declare const dWeb: {
442
452
  isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
443
453
  legacyCopy: (text: string) => boolean;
444
454
  };
455
+ rsa: {
456
+ generateKeys: () => Promise<{
457
+ publicKey: string | undefined;
458
+ publicKeyPem: string;
459
+ privateKey: string | undefined;
460
+ privateKeyPem: string;
461
+ } | undefined>;
462
+ format_pem_key: (pemContent?: string, label?: string) => string;
463
+ format_pem_content: (buffer: ArrayBuffer) => string | undefined;
464
+ encrypt: (data: string, publicKey: string) => Promise<string | undefined>;
465
+ decrypt: (encryptedData: string, privateKey: string) => Promise<string | undefined>;
466
+ };
445
467
  };
446
468
  export { dUtil, dHook, dWeb, dMini, dCdn, dStore, dJoker, dTracker, dLogger };
447
469
  declare const _default: {
@@ -611,6 +633,7 @@ declare const _default: {
611
633
  };
612
634
  uuid: (len?: number, radix?: number) => string;
613
635
  guid: (len: number, prefix?: boolean, sep?: string) => string;
636
+ getHexString: (len: number) => string;
614
637
  keyNumber: string;
615
638
  keyLower: string;
616
639
  keyUpper: string;
@@ -750,6 +773,10 @@ declare const _default: {
750
773
  uint8ArrayToString: (arr: any) => string;
751
774
  stringToArrayBuffer: (str: any) => ArrayBuffer;
752
775
  arrayBufferToString: (data: ArrayBuffer, base64?: boolean) => string;
776
+ strToHexString: (str: string) => string;
777
+ hexStringToStr: (hexstr: string) => string;
778
+ str2ab: (str?: string, base64?: boolean) => ArrayBuffer;
779
+ ab2str: (data: ArrayBuffer, base64?: boolean) => string;
753
780
  };
754
781
  Event: typeof Event;
755
782
  Http: typeof Http;
@@ -787,5 +814,17 @@ declare const _default: {
787
814
  fail?: ((error: Error) => void) | undefined;
788
815
  }) => Promise<Uint8Array | undefined>;
789
816
  };
817
+ rsa: {
818
+ generateKeys: () => Promise<{
819
+ publicKey: string | undefined;
820
+ publicKeyPem: string;
821
+ privateKey: string | undefined;
822
+ privateKeyPem: string;
823
+ } | undefined>;
824
+ format_pem_key: (pemContent?: string, label?: string) => string;
825
+ format_pem_content: (buffer: ArrayBuffer) => string | undefined;
826
+ encrypt: (data: string, publicKey: string) => Promise<string | undefined>;
827
+ decrypt: (encryptedData: string, privateKey: string) => Promise<string | undefined>;
828
+ };
790
829
  };
791
830
  export default _default;
@@ -1,9 +1,15 @@
1
1
  declare function uint8ArrayToString(arr: any): string;
2
2
  declare function stringToArrayBuffer(str: any): ArrayBuffer;
3
3
  declare function arrayBufferToString(data: ArrayBuffer, base64?: boolean): string;
4
+ declare function strToHexString(str: string): string;
5
+ declare function hexStringToStr(hexstr: string): string;
4
6
  declare const _default: {
5
7
  uint8ArrayToString: typeof uint8ArrayToString;
6
8
  stringToArrayBuffer: typeof stringToArrayBuffer;
7
9
  arrayBufferToString: typeof arrayBufferToString;
10
+ strToHexString: typeof strToHexString;
11
+ hexStringToStr: typeof hexStringToStr;
12
+ str2ab: (str?: string, base64?: boolean) => ArrayBuffer;
13
+ ab2str: (data: ArrayBuffer, base64?: boolean) => string;
8
14
  };
9
15
  export default _default;
@@ -6,6 +6,7 @@ declare const _default: {
6
6
  };
7
7
  uuid: (len?: number, radix?: number) => string;
8
8
  guid: (len: number, prefix?: boolean, sep?: string) => string;
9
+ getHexString: (len: number) => string;
9
10
  keyNumber: string;
10
11
  keyLower: string;
11
12
  keyUpper: string;
@@ -7,8 +7,9 @@ import { Ddan } from '../../../typings';
7
7
  * @returns
8
8
  */
9
9
  declare function debounce(func: Ddan.Action, wait?: number, options?: Ddan.IDebounceOption): {
10
- (...debouncedArgs: any[]): any;
10
+ (...args: any[]): any;
11
11
  cancel: () => void;
12
12
  flush: () => any;
13
+ pending: () => boolean;
13
14
  };
14
15
  export default debounce;
@@ -7,8 +7,9 @@ import { Ddan } from "../../../typings";
7
7
  * @returns
8
8
  */
9
9
  declare function throttle(func: Ddan.Action, wait?: number, options?: Omit<Ddan.IDebounceOption, 'maxWait'>): {
10
- (...debouncedArgs: any[]): any;
10
+ (...args: any[]): any;
11
11
  cancel: () => void;
12
12
  flush: () => any;
13
+ pending: () => boolean;
13
14
  };
14
15
  export default throttle;
@@ -0,0 +1,13 @@
1
+ declare const _default: {
2
+ generateKeys: () => Promise<{
3
+ publicKey: string | undefined;
4
+ publicKeyPem: string;
5
+ privateKey: string | undefined;
6
+ privateKeyPem: string;
7
+ } | undefined>;
8
+ format_pem_key: (pemContent?: string, label?: string) => string;
9
+ format_pem_content: (buffer: ArrayBuffer) => string | undefined;
10
+ encrypt: (data: string, publicKey: string) => Promise<string | undefined>;
11
+ decrypt: (encryptedData: string, privateKey: string) => Promise<string | undefined>;
12
+ };
13
+ export default _default;
@@ -18,6 +18,7 @@ export declare namespace Ddan {
18
18
  type Result<T = any> = T | Promise<T>;
19
19
  type SafeResult<T = any> = [any, undefined] | [null, T];
20
20
  type PSafeResult<T = any> = Promise<SafeResult<T>>;
21
+ type noop = (...args: any[]) => any;
21
22
  type ObjToKeyValUnion<T> = {
22
23
  [K in keyof T]: {
23
24
  key: K;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",