spd-lib 1.2.9 → 1.3.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/index.d.mts ADDED
@@ -0,0 +1,213 @@
1
+ import sodium from 'libsodium-wrappers';
2
+
3
+ type HashAlgorithm = 'sha3-512' | 'sha256' | 'sha512';
4
+ type SupportedDataType = 'string' | 'number' | 'boolean' | 'object' | 'Array' | 'Uint8Array' | 'Uint16Array' | 'Uint32Array' | 'BigInt64Array' | 'BigUint64Array' | 'Float32Array' | 'Float64Array' | 'Map' | 'Set' | 'Date' | 'RegExp' | 'Error';
5
+ interface EncryptedDataEntry {
6
+ dataName: string;
7
+ nonce: Buffer;
8
+ data: Buffer;
9
+ hash: string;
10
+ dataType: SupportedDataType;
11
+ }
12
+ interface SerializedDataEntry {
13
+ dataName: string;
14
+ nonce: number[];
15
+ data: number[];
16
+ hash: string;
17
+ dataType: SupportedDataType;
18
+ }
19
+ interface SPDPayload {
20
+ data: SerializedDataEntry[];
21
+ encryptedSalt: number[];
22
+ saltNonce: number[];
23
+ wrapSalt: number[];
24
+ version: number;
25
+ hashAlgorithm?: string;
26
+ argon2Memory?: number;
27
+ argon2Time?: number;
28
+ }
29
+ interface SPDLegacyPayload {
30
+ data: SerializedDataEntry[];
31
+ salt: number[];
32
+ }
33
+ interface WrappedPayload {
34
+ payload: Buffer;
35
+ mac: Buffer;
36
+ }
37
+ interface SerializedWrappedPayload {
38
+ payload: number[];
39
+ mac: number[];
40
+ }
41
+ interface PQCKey {
42
+ privateKey: Uint8Array;
43
+ publicKey?: Uint8Array;
44
+ }
45
+ interface PQCKeyResult {
46
+ pqcKey: PQCKey;
47
+ salt: Uint8Array;
48
+ }
49
+ interface PBKResult {
50
+ pbk: Buffer;
51
+ salt: Uint8Array;
52
+ }
53
+ interface EncryptedSaltResult {
54
+ encryptedSalt: number[];
55
+ saltNonce: number[];
56
+ wrapSalt: number[];
57
+ }
58
+ interface DataInput {
59
+ name: string;
60
+ data: unknown;
61
+ }
62
+ interface SPDChunkManifest {
63
+ totalChunks: number;
64
+ chunkSize: number;
65
+ totalBytes: number;
66
+ version: number;
67
+ }
68
+ type TypedArray = Uint8Array | Uint16Array | Uint32Array | BigInt64Array | BigUint64Array | Float32Array | Float64Array;
69
+ type SupportedValue = string | number | boolean | unknown[] | TypedArray | Map<unknown, unknown> | Set<unknown> | Date | RegExp | Error | Record<string, unknown>;
70
+
71
+ declare class SPD {
72
+ private data;
73
+ private keyPair?;
74
+ private userKey?;
75
+ private macKey?;
76
+ private salt?;
77
+ private hash;
78
+ private compressionLevel;
79
+ private assertReady;
80
+ init(): Promise<void>;
81
+ changePasscode(oldPasscode: string, newPasscode: string): Promise<void>;
82
+ /**
83
+ * Derives a 512-bit master secret via Argon2id and splits it into:
84
+ * aeadKey (32 B) — XChaCha20-Poly1305 encryption key (256-bit, 128-bit PQ)
85
+ * macKey (32 B) — HMAC-SHA3-512 authentication key (domain-separated)
86
+ */
87
+ static deriveKeys(passcode: string, salt: Uint8Array, memory?: number, time?: number): Promise<{
88
+ aeadKey: Uint8Array;
89
+ macKey: Uint8Array;
90
+ }>;
91
+ private static computeMAC;
92
+ checkPasscodeStrength(passcode: string): boolean;
93
+ setPassKey(passcode: string): Promise<void>;
94
+ setHash(hash?: HashAlgorithm): void;
95
+ setCompressionLevel(level?: number): void;
96
+ getSodium(): typeof sodium;
97
+ sanitizeName(dataName: string): string;
98
+ addData(dataName: string, value: unknown): Promise<void>;
99
+ addMany(items: DataInput[]): Promise<void>;
100
+ extractData(): Promise<Record<string, unknown>>;
101
+ destroy(): void;
102
+ clearCache(): void;
103
+ saveToFile(outputPath: string, passcode: string): Promise<void>;
104
+ /**
105
+ * Saves to file using streaming I/O — supports files larger than 2 GB.
106
+ * Format: [8B LE uint64 plaintext length][64B HMAC-SHA3-512][zlib(plaintext)]
107
+ */
108
+ saveToFileStreaming(outputPath: string, passcode: string): Promise<void>;
109
+ saveData(passcode?: string): Promise<string>;
110
+ /**
111
+ * Splits the payload into base64 chunks for chunked internet transfer.
112
+ * The manifest (last element) records totalChunks/totalBytes for validation.
113
+ * Reassemble with `SPD.loadFromChunks(chunks, passcode)`.
114
+ */
115
+ saveDataChunked(passcode: string, chunkSize?: number): Promise<string[]>;
116
+ static loadFromFile(filePath: string, passcode: string): Promise<SPD>;
117
+ static loadFromFileStreaming(filePath: string, passcode: string): Promise<SPD>;
118
+ static loadFromString(data: string, passcode: string): Promise<SPD>;
119
+ static loadFromChunks(chunks: string[], passcode: string): Promise<SPD>;
120
+ static derivePBK(passcode: string, salt: Uint8Array, memory?: number, time?: number): Promise<PBKResult>;
121
+ static decryptSalt(encryptedSalt: number[], saltNonce: number[], wrapSalt: number[], passcode: string, memory?: number, time?: number): Promise<Uint8Array>;
122
+ static encryptSalt(salt: Uint8Array, passcode: string): Promise<EncryptedSaltResult>;
123
+ static toBase64(data: Uint8Array | Buffer): string;
124
+ static fromBase64(data: string): Uint8Array;
125
+ private buildSerializedPayload;
126
+ private static parseSerializedPayload;
127
+ private buildBinaryPayload;
128
+ private static parseBinaryPayload;
129
+ private detectType;
130
+ private isTypedArray;
131
+ private isCollectionType;
132
+ private convertInputToString;
133
+ private convertStringToInput;
134
+ }
135
+
136
+ /**
137
+ * Legacy SPD class for backwards compatibility.
138
+ * Uses older cryptographic methods (crypto_secretbox instead of AEAD).
139
+ */
140
+ declare class SPDLegacy {
141
+ private data;
142
+ private keyPair?;
143
+ private userKey?;
144
+ private salt?;
145
+ constructor();
146
+ init(): Promise<void>;
147
+ setPassKey(passcode: string): Promise<void>;
148
+ addData(dataName: string, value: unknown): Promise<void>;
149
+ saveToFile(outputPath: string): void;
150
+ static loadFromFile(filePath: string, passcode: string): Promise<SPDLegacy>;
151
+ extractData(): Promise<Record<string, unknown>>;
152
+ static derivePBK(passcode: string, salt: Uint8Array): Promise<PBKResult>;
153
+ saveData(): Buffer;
154
+ static loadFromString(data: string, passcode: string): Promise<SPDLegacy>;
155
+ private convertPasscodeToPQCKeySalted;
156
+ convertPasscodeToPQCKey(passcode: string): Promise<PQCKeyResult>;
157
+ private detectType;
158
+ private isTypedArray;
159
+ private isCollectionType;
160
+ private isSpecialType;
161
+ private convertInputToString;
162
+ private convertStringToInput;
163
+ }
164
+
165
+ /**
166
+ * In-memory key vault with automatic expiration.
167
+ * Keys are automatically deleted after the specified timeout.
168
+ */
169
+ declare class SPDVault {
170
+ private keys;
171
+ private timers;
172
+ private timeoutMs;
173
+ constructor(timeoutMs?: number);
174
+ /**
175
+ * Generate a random key string.
176
+ */
177
+ private generateRandomKey;
178
+ /**
179
+ * Reset the expiration timer for a key.
180
+ */
181
+ private resetTimer;
182
+ /**
183
+ * Generate and store a new random key.
184
+ */
185
+ genKey(keyId?: string): void;
186
+ /**
187
+ * Store a custom key.
188
+ */
189
+ pushKey(keyId?: string, key?: string): void;
190
+ /**
191
+ * Retrieve a key by ID.
192
+ * Returns undefined if the key doesn't exist.
193
+ */
194
+ pullKey(keyId?: string): string | undefined;
195
+ /**
196
+ * Delete a key by ID.
197
+ */
198
+ destroyKey(keyId?: string): void;
199
+ /**
200
+ * Update a key if the old value matches.
201
+ */
202
+ updateKey(keyId: string, oldValue?: string, newValue?: string): void;
203
+ /**
204
+ * Stop all expiration timers.
205
+ */
206
+ stop(): void;
207
+ /**
208
+ * Clear all keys and timers.
209
+ */
210
+ destroy(): void;
211
+ }
212
+
213
+ export { type DataInput, type EncryptedDataEntry, type EncryptedSaltResult, type HashAlgorithm, type PBKResult, type PQCKey, type PQCKeyResult, SPD, type SPDChunkManifest, SPDLegacy, type SPDLegacyPayload, type SPDPayload, SPDVault, SPDLegacy as SPD_LEG, SPDVault as SPD_Vault, type SerializedDataEntry, type SerializedWrappedPayload, type SupportedDataType, type SupportedValue, type TypedArray, type WrappedPayload };
package/index.d.ts ADDED
@@ -0,0 +1,213 @@
1
+ import sodium from 'libsodium-wrappers';
2
+
3
+ type HashAlgorithm = 'sha3-512' | 'sha256' | 'sha512';
4
+ type SupportedDataType = 'string' | 'number' | 'boolean' | 'object' | 'Array' | 'Uint8Array' | 'Uint16Array' | 'Uint32Array' | 'BigInt64Array' | 'BigUint64Array' | 'Float32Array' | 'Float64Array' | 'Map' | 'Set' | 'Date' | 'RegExp' | 'Error';
5
+ interface EncryptedDataEntry {
6
+ dataName: string;
7
+ nonce: Buffer;
8
+ data: Buffer;
9
+ hash: string;
10
+ dataType: SupportedDataType;
11
+ }
12
+ interface SerializedDataEntry {
13
+ dataName: string;
14
+ nonce: number[];
15
+ data: number[];
16
+ hash: string;
17
+ dataType: SupportedDataType;
18
+ }
19
+ interface SPDPayload {
20
+ data: SerializedDataEntry[];
21
+ encryptedSalt: number[];
22
+ saltNonce: number[];
23
+ wrapSalt: number[];
24
+ version: number;
25
+ hashAlgorithm?: string;
26
+ argon2Memory?: number;
27
+ argon2Time?: number;
28
+ }
29
+ interface SPDLegacyPayload {
30
+ data: SerializedDataEntry[];
31
+ salt: number[];
32
+ }
33
+ interface WrappedPayload {
34
+ payload: Buffer;
35
+ mac: Buffer;
36
+ }
37
+ interface SerializedWrappedPayload {
38
+ payload: number[];
39
+ mac: number[];
40
+ }
41
+ interface PQCKey {
42
+ privateKey: Uint8Array;
43
+ publicKey?: Uint8Array;
44
+ }
45
+ interface PQCKeyResult {
46
+ pqcKey: PQCKey;
47
+ salt: Uint8Array;
48
+ }
49
+ interface PBKResult {
50
+ pbk: Buffer;
51
+ salt: Uint8Array;
52
+ }
53
+ interface EncryptedSaltResult {
54
+ encryptedSalt: number[];
55
+ saltNonce: number[];
56
+ wrapSalt: number[];
57
+ }
58
+ interface DataInput {
59
+ name: string;
60
+ data: unknown;
61
+ }
62
+ interface SPDChunkManifest {
63
+ totalChunks: number;
64
+ chunkSize: number;
65
+ totalBytes: number;
66
+ version: number;
67
+ }
68
+ type TypedArray = Uint8Array | Uint16Array | Uint32Array | BigInt64Array | BigUint64Array | Float32Array | Float64Array;
69
+ type SupportedValue = string | number | boolean | unknown[] | TypedArray | Map<unknown, unknown> | Set<unknown> | Date | RegExp | Error | Record<string, unknown>;
70
+
71
+ declare class SPD {
72
+ private data;
73
+ private keyPair?;
74
+ private userKey?;
75
+ private macKey?;
76
+ private salt?;
77
+ private hash;
78
+ private compressionLevel;
79
+ private assertReady;
80
+ init(): Promise<void>;
81
+ changePasscode(oldPasscode: string, newPasscode: string): Promise<void>;
82
+ /**
83
+ * Derives a 512-bit master secret via Argon2id and splits it into:
84
+ * aeadKey (32 B) — XChaCha20-Poly1305 encryption key (256-bit, 128-bit PQ)
85
+ * macKey (32 B) — HMAC-SHA3-512 authentication key (domain-separated)
86
+ */
87
+ static deriveKeys(passcode: string, salt: Uint8Array, memory?: number, time?: number): Promise<{
88
+ aeadKey: Uint8Array;
89
+ macKey: Uint8Array;
90
+ }>;
91
+ private static computeMAC;
92
+ checkPasscodeStrength(passcode: string): boolean;
93
+ setPassKey(passcode: string): Promise<void>;
94
+ setHash(hash?: HashAlgorithm): void;
95
+ setCompressionLevel(level?: number): void;
96
+ getSodium(): typeof sodium;
97
+ sanitizeName(dataName: string): string;
98
+ addData(dataName: string, value: unknown): Promise<void>;
99
+ addMany(items: DataInput[]): Promise<void>;
100
+ extractData(): Promise<Record<string, unknown>>;
101
+ destroy(): void;
102
+ clearCache(): void;
103
+ saveToFile(outputPath: string, passcode: string): Promise<void>;
104
+ /**
105
+ * Saves to file using streaming I/O — supports files larger than 2 GB.
106
+ * Format: [8B LE uint64 plaintext length][64B HMAC-SHA3-512][zlib(plaintext)]
107
+ */
108
+ saveToFileStreaming(outputPath: string, passcode: string): Promise<void>;
109
+ saveData(passcode?: string): Promise<string>;
110
+ /**
111
+ * Splits the payload into base64 chunks for chunked internet transfer.
112
+ * The manifest (last element) records totalChunks/totalBytes for validation.
113
+ * Reassemble with `SPD.loadFromChunks(chunks, passcode)`.
114
+ */
115
+ saveDataChunked(passcode: string, chunkSize?: number): Promise<string[]>;
116
+ static loadFromFile(filePath: string, passcode: string): Promise<SPD>;
117
+ static loadFromFileStreaming(filePath: string, passcode: string): Promise<SPD>;
118
+ static loadFromString(data: string, passcode: string): Promise<SPD>;
119
+ static loadFromChunks(chunks: string[], passcode: string): Promise<SPD>;
120
+ static derivePBK(passcode: string, salt: Uint8Array, memory?: number, time?: number): Promise<PBKResult>;
121
+ static decryptSalt(encryptedSalt: number[], saltNonce: number[], wrapSalt: number[], passcode: string, memory?: number, time?: number): Promise<Uint8Array>;
122
+ static encryptSalt(salt: Uint8Array, passcode: string): Promise<EncryptedSaltResult>;
123
+ static toBase64(data: Uint8Array | Buffer): string;
124
+ static fromBase64(data: string): Uint8Array;
125
+ private buildSerializedPayload;
126
+ private static parseSerializedPayload;
127
+ private buildBinaryPayload;
128
+ private static parseBinaryPayload;
129
+ private detectType;
130
+ private isTypedArray;
131
+ private isCollectionType;
132
+ private convertInputToString;
133
+ private convertStringToInput;
134
+ }
135
+
136
+ /**
137
+ * Legacy SPD class for backwards compatibility.
138
+ * Uses older cryptographic methods (crypto_secretbox instead of AEAD).
139
+ */
140
+ declare class SPDLegacy {
141
+ private data;
142
+ private keyPair?;
143
+ private userKey?;
144
+ private salt?;
145
+ constructor();
146
+ init(): Promise<void>;
147
+ setPassKey(passcode: string): Promise<void>;
148
+ addData(dataName: string, value: unknown): Promise<void>;
149
+ saveToFile(outputPath: string): void;
150
+ static loadFromFile(filePath: string, passcode: string): Promise<SPDLegacy>;
151
+ extractData(): Promise<Record<string, unknown>>;
152
+ static derivePBK(passcode: string, salt: Uint8Array): Promise<PBKResult>;
153
+ saveData(): Buffer;
154
+ static loadFromString(data: string, passcode: string): Promise<SPDLegacy>;
155
+ private convertPasscodeToPQCKeySalted;
156
+ convertPasscodeToPQCKey(passcode: string): Promise<PQCKeyResult>;
157
+ private detectType;
158
+ private isTypedArray;
159
+ private isCollectionType;
160
+ private isSpecialType;
161
+ private convertInputToString;
162
+ private convertStringToInput;
163
+ }
164
+
165
+ /**
166
+ * In-memory key vault with automatic expiration.
167
+ * Keys are automatically deleted after the specified timeout.
168
+ */
169
+ declare class SPDVault {
170
+ private keys;
171
+ private timers;
172
+ private timeoutMs;
173
+ constructor(timeoutMs?: number);
174
+ /**
175
+ * Generate a random key string.
176
+ */
177
+ private generateRandomKey;
178
+ /**
179
+ * Reset the expiration timer for a key.
180
+ */
181
+ private resetTimer;
182
+ /**
183
+ * Generate and store a new random key.
184
+ */
185
+ genKey(keyId?: string): void;
186
+ /**
187
+ * Store a custom key.
188
+ */
189
+ pushKey(keyId?: string, key?: string): void;
190
+ /**
191
+ * Retrieve a key by ID.
192
+ * Returns undefined if the key doesn't exist.
193
+ */
194
+ pullKey(keyId?: string): string | undefined;
195
+ /**
196
+ * Delete a key by ID.
197
+ */
198
+ destroyKey(keyId?: string): void;
199
+ /**
200
+ * Update a key if the old value matches.
201
+ */
202
+ updateKey(keyId: string, oldValue?: string, newValue?: string): void;
203
+ /**
204
+ * Stop all expiration timers.
205
+ */
206
+ stop(): void;
207
+ /**
208
+ * Clear all keys and timers.
209
+ */
210
+ destroy(): void;
211
+ }
212
+
213
+ export { type DataInput, type EncryptedDataEntry, type EncryptedSaltResult, type HashAlgorithm, type PBKResult, type PQCKey, type PQCKeyResult, SPD, type SPDChunkManifest, SPDLegacy, type SPDLegacyPayload, type SPDPayload, SPDVault, SPDLegacy as SPD_LEG, SPDVault as SPD_Vault, type SerializedDataEntry, type SerializedWrappedPayload, type SupportedDataType, type SupportedValue, type TypedArray, type WrappedPayload };