cipher-kit 2.0.0 → 2.1.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/README.md +25 -27
- package/dist/{chunk-3UX5MZ2P.cjs → chunk-BHG5RSUV.cjs} +22 -20
- package/dist/chunk-BHG5RSUV.cjs.map +1 -0
- package/dist/{chunk-4MFF6V3R.js → chunk-CRTOKS3Q.js} +76 -41
- package/dist/chunk-CRTOKS3Q.js.map +1 -0
- package/dist/{chunk-N2EW2FDZ.cjs → chunk-HMTHK2IY.cjs} +183 -148
- package/dist/chunk-HMTHK2IY.cjs.map +1 -0
- package/dist/{chunk-ACFPMIXO.js → chunk-RAEBT46G.js} +75 -40
- package/dist/chunk-RAEBT46G.js.map +1 -0
- package/dist/{chunk-FKSYSPJR.js → chunk-RUTGDMVR.js} +21 -20
- package/dist/chunk-RUTGDMVR.js.map +1 -0
- package/dist/{chunk-CVCDAHDW.cjs → chunk-UVEMRK5F.cjs} +189 -154
- package/dist/chunk-UVEMRK5F.cjs.map +1 -0
- package/dist/{export-BMvZq46v.d.ts → export-5hmOiU0J.d.cts} +194 -20
- package/dist/{export-CQNsJFh_.d.cts → export-BF9wW56f.d.ts} +194 -20
- package/dist/{export-llM6c7Do.d.ts → export-DVERZibl.d.cts} +194 -20
- package/dist/{export-55tHE0Bw.d.cts → export-w8sBcKXw.d.ts} +194 -20
- package/dist/index.cjs +12 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +33 -33
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-EHuJC5QQ.d.cts → validate-B3uHoP8n.d.cts} +26 -38
- package/dist/{validate-EHuJC5QQ.d.ts → validate-B3uHoP8n.d.ts} +26 -38
- package/dist/web-api.cjs +33 -33
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +6 -7
- package/dist/chunk-3UX5MZ2P.cjs.map +0 -1
- package/dist/chunk-4MFF6V3R.js.map +0 -1
- package/dist/chunk-ACFPMIXO.js.map +0 -1
- package/dist/chunk-CVCDAHDW.cjs.map +0 -1
- package/dist/chunk-FKSYSPJR.js.map +0 -1
- package/dist/chunk-N2EW2FDZ.cjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SecretKey, R as Result, d as CreateSecretKeyOptions, e as EncryptOptions, f as DecryptOptions, H as HashOptions, g as HashPasswordOptions, V as VerifyPasswordOptions, b as Encoding } from './validate-
|
|
1
|
+
import { S as SecretKey, R as Result, d as CreateSecretKeyOptions, e as EncryptOptions, f as DecryptOptions, H as HashOptions, g as HashPasswordOptions, V as VerifyPasswordOptions, b as Encoding } from './validate-B3uHoP8n.js';
|
|
2
2
|
import { Buffer } from 'node:buffer';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -9,6 +9,13 @@ import { Buffer } from 'node:buffer';
|
|
|
9
9
|
*
|
|
10
10
|
* @param x - The value to check.
|
|
11
11
|
* @returns True if the value is a SecretKey object for Node.js, false otherwise.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* isNodeSecretKey(nodeKey); // true
|
|
16
|
+
* isNodeSecretKey(webKey); // false
|
|
17
|
+
* isNodeSecretKey({}); // false
|
|
18
|
+
* ```
|
|
12
19
|
*/
|
|
13
20
|
declare function isNodeSecretKey(x: unknown): x is SecretKey<"node">;
|
|
14
21
|
/**
|
|
@@ -19,6 +26,14 @@ declare function isNodeSecretKey(x: unknown): x is SecretKey<"node">;
|
|
|
19
26
|
* The chance of two pets getting the same name tag is practically zero, and it's very hard to guess!
|
|
20
27
|
*
|
|
21
28
|
* @returns A `Result` containing the UUID string or an error.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const {result, error, success} = tryGenerateUuid();
|
|
33
|
+
*
|
|
34
|
+
* if (success) console.log(result); // "..." (a UUID string)
|
|
35
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
36
|
+
* ```
|
|
22
37
|
*/
|
|
23
38
|
declare function tryGenerateUuid(): Result<string>;
|
|
24
39
|
/**
|
|
@@ -30,6 +45,11 @@ declare function tryGenerateUuid(): Result<string>;
|
|
|
30
45
|
*
|
|
31
46
|
* @returns A UUID string.
|
|
32
47
|
* @throws {Error} If UUID generation fails.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const uuid = generateUuid(); // "..." (a UUID string)
|
|
52
|
+
* ```
|
|
33
53
|
*/
|
|
34
54
|
declare function generateUuid(): string;
|
|
35
55
|
/**
|
|
@@ -41,12 +61,20 @@ declare function generateUuid(): string;
|
|
|
41
61
|
* Imagine you want to create a special key for future use to lock your treasure box (data).
|
|
42
62
|
* So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.
|
|
43
63
|
*
|
|
44
|
-
* @param secret - The input string to derive the `SecretKey` from.
|
|
64
|
+
* @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.
|
|
45
65
|
* @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).
|
|
46
66
|
* @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).
|
|
47
67
|
* @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).
|
|
48
68
|
* @param options.info - An info string (default: `'cipher-kit'`).
|
|
49
69
|
* @returns A `Result` containing the derived `SecretKey` or an error.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const {result, error, success} = tryCreateSecretKey("my-secret");
|
|
74
|
+
*
|
|
75
|
+
* if (success) console.log(result); // SecretKey object
|
|
76
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
77
|
+
* ```
|
|
50
78
|
*/
|
|
51
79
|
declare function tryCreateSecretKey(secret: string, options?: CreateSecretKeyOptions): Result<{
|
|
52
80
|
result: SecretKey<"node">;
|
|
@@ -60,13 +88,18 @@ declare function tryCreateSecretKey(secret: string, options?: CreateSecretKeyOpt
|
|
|
60
88
|
* Imagine you want to create a special key for future use to lock your treasure box (data).
|
|
61
89
|
* So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.
|
|
62
90
|
*
|
|
63
|
-
* @param secret - The input string to derive the `SecretKey` from.
|
|
91
|
+
* @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.
|
|
64
92
|
* @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).
|
|
65
93
|
* @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).
|
|
66
94
|
* @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).
|
|
67
95
|
* @param options.info - An info string (default: `'cipher-kit'`).
|
|
68
96
|
* @returns The derived `SecretKey`.
|
|
69
97
|
* @throws {Error} If key derivation fails.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const secretKey = createSecretKey("my-secret"); // SecretKey object
|
|
102
|
+
* ```
|
|
70
103
|
*/
|
|
71
104
|
declare function createSecretKey(secret: string, options?: CreateSecretKeyOptions): SecretKey<"node">;
|
|
72
105
|
/**
|
|
@@ -80,8 +113,17 @@ declare function createSecretKey(secret: string, options?: CreateSecretKeyOption
|
|
|
80
113
|
*
|
|
81
114
|
* @param data - A UTF-8 string to encrypt.
|
|
82
115
|
* @param secretKey - The `SecretKey` object used for encryption.
|
|
83
|
-
* @param options.
|
|
116
|
+
* @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).
|
|
84
117
|
* @returns A `Result` containing the encrypted string in the specified format or an error.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const secretKey = createSecretKey("my-secret");
|
|
122
|
+
* const {result, error, success} = tryEncrypt("Hello, World!", secretKey);
|
|
123
|
+
*
|
|
124
|
+
* if (success) console.log(result); // "iv.cipher.tag." (Encrypted string)
|
|
125
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
126
|
+
* ```
|
|
85
127
|
*/
|
|
86
128
|
declare function tryEncrypt(data: string, secretKey: SecretKey<"node">, options?: EncryptOptions): Result<string>;
|
|
87
129
|
/**
|
|
@@ -95,9 +137,15 @@ declare function tryEncrypt(data: string, secretKey: SecretKey<"node">, options?
|
|
|
95
137
|
*
|
|
96
138
|
* @param data - A UTF-8 string to encrypt.
|
|
97
139
|
* @param secretKey - The `SecretKey` object used for encryption.
|
|
98
|
-
* @param options.
|
|
140
|
+
* @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).
|
|
99
141
|
* @returns The encrypted string in the specified format.
|
|
100
142
|
* @throws {Error} If the input data or key is invalid, or if encryption fails.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const secretKey = createSecretKey("my-secret");
|
|
147
|
+
* const encrypted = encrypt("Hello, World!", secretKey); // "iv.cipher.tag." (Encrypted string)
|
|
148
|
+
* ```
|
|
101
149
|
*/
|
|
102
150
|
declare function encrypt(data: string, secretKey: SecretKey<"node">, options?: EncryptOptions): string;
|
|
103
151
|
/**
|
|
@@ -111,8 +159,18 @@ declare function encrypt(data: string, secretKey: SecretKey<"node">, options?: E
|
|
|
111
159
|
*
|
|
112
160
|
* @param encrypted - The input string to decrypt, in the format "iv.cipher.tag.".
|
|
113
161
|
* @param secretKey - The `SecretKey` object used for decryption.
|
|
114
|
-
* @param options.
|
|
162
|
+
* @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).
|
|
115
163
|
* @returns A `Result` containing the decrypted UTF-8 string or an error.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* const secretKey = createSecretKey("my-secret");
|
|
168
|
+
* const encrypted = encrypt("Hello, World!", secretKey);
|
|
169
|
+
* const {result, error, success} = tryDecrypt(encrypted, secretKey);
|
|
170
|
+
*
|
|
171
|
+
* if (success) console.log(result); // "Hello, World!" (Decrypted string)
|
|
172
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
173
|
+
* ```
|
|
116
174
|
*/
|
|
117
175
|
declare function tryDecrypt(encrypted: string, secretKey: SecretKey<"node">, options?: DecryptOptions): Result<string>;
|
|
118
176
|
/**
|
|
@@ -126,9 +184,16 @@ declare function tryDecrypt(encrypted: string, secretKey: SecretKey<"node">, opt
|
|
|
126
184
|
*
|
|
127
185
|
* @param encrypted - The input string to decrypt, in the format "iv.cipher.tag.".
|
|
128
186
|
* @param secretKey - The `SecretKey` object used for decryption.
|
|
129
|
-
* @param options.
|
|
187
|
+
* @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).
|
|
130
188
|
* @returns The decrypted UTF-8 string.
|
|
131
189
|
* @throws {Error} If the input data or key is invalid, or if decryption fails.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const secretKey = createSecretKey("my-secret");
|
|
194
|
+
* const encrypted = encrypt("Hello, World!", secretKey);
|
|
195
|
+
* const decrypted = decrypt(encrypted, secretKey); // "Hello, World!" (Decrypted string)
|
|
196
|
+
* ```
|
|
132
197
|
*/
|
|
133
198
|
declare function decrypt(encrypted: string, secretKey: SecretKey<"node">, options?: DecryptOptions): string;
|
|
134
199
|
/**
|
|
@@ -143,10 +208,20 @@ declare function decrypt(encrypted: string, secretKey: SecretKey<"node">, option
|
|
|
143
208
|
* So, you take a picture of your toy box (convert it to JSON), and scramble that with
|
|
144
209
|
* your special key, creating a jumbled code that only someone with the right key can read.
|
|
145
210
|
*
|
|
211
|
+
* @template T - The type of the plain object to encrypt.
|
|
146
212
|
* @param data - A plain object to encrypt.
|
|
147
213
|
* @param secretKey - The `SecretKey` object used for encryption.
|
|
148
|
-
* @param options.
|
|
214
|
+
* @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).
|
|
149
215
|
* @returns A `Result` containing the encrypted string in the specified format or an error.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* const secretKey = createSecretKey("my-secret");
|
|
220
|
+
* const {result, error, success} = tryEncryptObj({ a: 1 }, secretKey);
|
|
221
|
+
*
|
|
222
|
+
* if (success) console.log(result); // "iv.cipher.tag." (Encrypted string)
|
|
223
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
224
|
+
* ```
|
|
150
225
|
*/
|
|
151
226
|
declare function tryEncryptObj<T extends object = Record<string, unknown>>(data: T, secretKey: SecretKey<"node">, options?: EncryptOptions): Result<string>;
|
|
152
227
|
/**
|
|
@@ -161,11 +236,18 @@ declare function tryEncryptObj<T extends object = Record<string, unknown>>(data:
|
|
|
161
236
|
* So, you take a picture of your toy box (convert it to JSON), and scramble that with
|
|
162
237
|
* your special key, creating a jumbled code that only someone with the right key can read.
|
|
163
238
|
*
|
|
239
|
+
* @template T - The type of the plain object to encrypt.
|
|
164
240
|
* @param data - A plain object to encrypt.
|
|
165
241
|
* @param secretKey - The `SecretKey` object used for encryption.
|
|
166
|
-
* @param options.
|
|
242
|
+
* @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).
|
|
167
243
|
* @returns The encrypted string in the specified format.
|
|
168
244
|
* @throws {Error} If the input data or key is invalid, or if encryption fails.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* const secretKey = createSecretKey("my-secret");
|
|
249
|
+
* const encrypted = encryptObj({ a: 1 }, secretKey); // "iv.cipher.tag." (Encrypted string)
|
|
250
|
+
* ```
|
|
169
251
|
*/
|
|
170
252
|
declare function encryptObj<T extends object = Record<string, unknown>>(data: T, secretKey: SecretKey<"node">, options?: EncryptOptions): string;
|
|
171
253
|
/**
|
|
@@ -180,8 +262,18 @@ declare function encryptObj<T extends object = Record<string, unknown>>(data: T,
|
|
|
180
262
|
* @template T - The expected shape of the decrypted object.
|
|
181
263
|
* @param encrypted - The encrypted string (format: `"iv.cipher.tag."`).
|
|
182
264
|
* @param secretKey - The `SecretKey` used for decryption.
|
|
183
|
-
* @param options.
|
|
265
|
+
* @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).
|
|
184
266
|
* @returns A `Result` with the decrypted object on success, or an error.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* const secretKey = createSecretKey("my-secret");
|
|
271
|
+
* const encrypted = encryptObj({ a: 1 }, secretKey);
|
|
272
|
+
* const {result, error, success} = tryDecryptObj<{ a: number }>(encrypted, secretKey);
|
|
273
|
+
*
|
|
274
|
+
* if (success) console.log(result); // { a: 1 } (Decrypted object)
|
|
275
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
276
|
+
* ```
|
|
185
277
|
*/
|
|
186
278
|
declare function tryDecryptObj<T extends object = Record<string, unknown>>(encrypted: string, secretKey: SecretKey<"node">, options?: DecryptOptions): Result<{
|
|
187
279
|
result: T;
|
|
@@ -198,9 +290,16 @@ declare function tryDecryptObj<T extends object = Record<string, unknown>>(encry
|
|
|
198
290
|
* @template T - The expected shape of the decrypted object.
|
|
199
291
|
* @param encrypted - The encrypted string (format: `"iv.cipher.tag."`).
|
|
200
292
|
* @param secretKey - The `SecretKey` used for decryption.
|
|
201
|
-
* @param options.
|
|
293
|
+
* @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).
|
|
202
294
|
* @returns The decrypted object.
|
|
203
295
|
* @throws {Error} If decryption or JSON parsing fails.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* const secretKey = createSecretKey("my-secret");
|
|
300
|
+
* const encrypted = encryptObj({ a: 1 }, secretKey);
|
|
301
|
+
* const obj = decryptObj<{ a: number }>(encrypted, secretKey); // obj.a === 1
|
|
302
|
+
* ```
|
|
204
303
|
*/
|
|
205
304
|
declare function decryptObj<T extends object = Record<string, unknown>>(encrypted: string, secretKey: SecretKey<"node">, options?: DecryptOptions): T;
|
|
206
305
|
/**
|
|
@@ -215,8 +314,16 @@ declare function decryptObj<T extends object = Record<string, unknown>>(encrypte
|
|
|
215
314
|
*
|
|
216
315
|
* @param data - The input string to hash.
|
|
217
316
|
* @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`, default: `'sha256'`).
|
|
218
|
-
* @param options.
|
|
317
|
+
* @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`, default: `'base64url'`).
|
|
219
318
|
* @returns A `Result` with the hash string or an error.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* const {result, error, success} = tryHash("my data");
|
|
323
|
+
*
|
|
324
|
+
* if (success) console.log(result); // "..." (Hashed string)
|
|
325
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
326
|
+
* ```
|
|
220
327
|
*/
|
|
221
328
|
declare function tryHash(data: string, options?: HashOptions): Result<string>;
|
|
222
329
|
/**
|
|
@@ -231,9 +338,14 @@ declare function tryHash(data: string, options?: HashOptions): Result<string>;
|
|
|
231
338
|
*
|
|
232
339
|
* @param data - The input string to hash.
|
|
233
340
|
* @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha256'`).
|
|
234
|
-
* @param options.
|
|
341
|
+
* @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).
|
|
235
342
|
* @returns The hashed string.
|
|
236
343
|
* @throws {Error} If input is invalid or hashing fails.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* const hash = hash("my data"); // "..." (Hashed string)
|
|
348
|
+
* ```
|
|
237
349
|
*/
|
|
238
350
|
declare function hash(data: string, options?: HashOptions): string;
|
|
239
351
|
/**
|
|
@@ -248,14 +360,22 @@ declare function hash(data: string, options?: HashOptions): string;
|
|
|
248
360
|
*
|
|
249
361
|
* @param password - The password to hash.
|
|
250
362
|
* @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).
|
|
251
|
-
* @param options.
|
|
363
|
+
* @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).
|
|
252
364
|
* @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).
|
|
253
365
|
* @param options.iterations - Number of iterations (default: `320000`, min: `1000`).
|
|
254
366
|
* @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).
|
|
255
|
-
* @returns A `Result` with `{
|
|
367
|
+
* @returns A `Result` with `{ result, salt }` or an error.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* const {result, salt, error, success} = tryHashPassword("my-password");
|
|
372
|
+
*
|
|
373
|
+
* if (success) console.log(result, salt); // "..." (Hashed password string), "..." (salt)
|
|
374
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
375
|
+
* ```
|
|
256
376
|
*/
|
|
257
377
|
declare function tryHashPassword(password: string, options?: HashPasswordOptions): Result<{
|
|
258
|
-
|
|
378
|
+
result: string;
|
|
259
379
|
salt: string;
|
|
260
380
|
}>;
|
|
261
381
|
/**
|
|
@@ -270,15 +390,20 @@ declare function tryHashPassword(password: string, options?: HashPasswordOptions
|
|
|
270
390
|
*
|
|
271
391
|
* @param password - The password to hash.
|
|
272
392
|
* @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).
|
|
273
|
-
* @param options.
|
|
393
|
+
* @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).
|
|
274
394
|
* @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).
|
|
275
395
|
* @param options.iterations - Number of iterations (default: `320000`, min: `1000`).
|
|
276
396
|
* @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).
|
|
277
|
-
* @returns `{
|
|
397
|
+
* @returns `{ result, salt }` for storage.
|
|
278
398
|
* @throws {Error} If inputs are invalid or hashing fails.
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* const { result, salt } = hashPassword("my-password"); // "..." (Hashed password string), "..." (salt)
|
|
403
|
+
* ```
|
|
279
404
|
*/
|
|
280
405
|
declare function hashPassword(password: string, options?: HashPasswordOptions): {
|
|
281
|
-
|
|
406
|
+
result: string;
|
|
282
407
|
salt: string;
|
|
283
408
|
};
|
|
284
409
|
/**
|
|
@@ -294,10 +419,18 @@ declare function hashPassword(password: string, options?: HashPasswordOptions):
|
|
|
294
419
|
* @param hashedPassword - The stored hash (encoded).
|
|
295
420
|
* @param salt - The stored salt (encoded).
|
|
296
421
|
* @param options.digest - Hash algorithm used during hashing (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).
|
|
297
|
-
* @param options.
|
|
422
|
+
* @param options.inputEncoding - Encoding of the stored hash and salt (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).
|
|
298
423
|
* @param options.iterations - Number of iterations used during hashing (default: `320000`).
|
|
299
424
|
* @param options.keyLength - Length of the derived key in bytes used during hashing (default: `64` bytes).
|
|
300
425
|
* @returns `true` if the password matches, otherwise `false`.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```ts
|
|
429
|
+
* const { result, salt } = hashPassword("my-password");
|
|
430
|
+
*
|
|
431
|
+
* verifyPassword("my-password", result, salt); // true
|
|
432
|
+
* verifyPassword("wrong-password", result, salt); // false
|
|
433
|
+
* ```
|
|
301
434
|
*/
|
|
302
435
|
declare function verifyPassword(password: string, hashedPassword: string, salt: string, options?: VerifyPasswordOptions): boolean;
|
|
303
436
|
/**
|
|
@@ -311,6 +444,14 @@ declare function verifyPassword(password: string, hashedPassword: string, salt:
|
|
|
311
444
|
* @param data - The input string to convert.
|
|
312
445
|
* @param inputEncoding - The encoding of the input string (default: `'utf8'`).
|
|
313
446
|
* @returns A `Result` with `{ result: Buffer }` or an error.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* const {result, error, success} = tryConvertStrToBytes("Hello", "utf8");
|
|
451
|
+
*
|
|
452
|
+
* if (success) console.log(result); // <Buffer ...>
|
|
453
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
454
|
+
* ```
|
|
314
455
|
*/
|
|
315
456
|
declare function tryConvertStrToBytes(data: string, inputEncoding?: Encoding): Result<{
|
|
316
457
|
result: Buffer;
|
|
@@ -327,6 +468,11 @@ declare function tryConvertStrToBytes(data: string, inputEncoding?: Encoding): R
|
|
|
327
468
|
* @param inputEncoding - The encoding of the input string (default: `'utf8'`).
|
|
328
469
|
* @returns A `Buffer` containing the bytes.
|
|
329
470
|
* @throws {Error} If input is invalid or conversion fails.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```ts
|
|
474
|
+
* const bytes = convertStrToBytes("Hello", "utf8"); // <Buffer ...>
|
|
475
|
+
* ```
|
|
330
476
|
*/
|
|
331
477
|
declare function convertStrToBytes(data: string, inputEncoding?: Encoding): Buffer;
|
|
332
478
|
/**
|
|
@@ -340,6 +486,15 @@ declare function convertStrToBytes(data: string, inputEncoding?: Encoding): Buff
|
|
|
340
486
|
* @param data - The `Buffer` to convert.
|
|
341
487
|
* @param outputEncoding - The output encoding (default: `'utf8'`).
|
|
342
488
|
* @returns A `Result` with the string or an error.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* const bytes = convertStrToBytes("Hello", "utf8"); // <Buffer ...>
|
|
493
|
+
* const {result, error, success} = tryConvertBytesToStr(bytes, "utf8");
|
|
494
|
+
*
|
|
495
|
+
* if (success) console.log(result); // "Hello"
|
|
496
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
497
|
+
* ```
|
|
343
498
|
*/
|
|
344
499
|
declare function tryConvertBytesToStr(data: Buffer, outputEncoding?: Encoding): Result<string>;
|
|
345
500
|
/**
|
|
@@ -354,6 +509,12 @@ declare function tryConvertBytesToStr(data: Buffer, outputEncoding?: Encoding):
|
|
|
354
509
|
* @param outputEncoding - The output encoding (default: `'utf8'`).
|
|
355
510
|
* @returns The encoded string.
|
|
356
511
|
* @throws {Error} If input is invalid or conversion fails.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```ts
|
|
515
|
+
* const bytes = convertStrToBytes("Hello", "utf8"); // <Buffer ...>
|
|
516
|
+
* const str = convertBytesToStr(bytes, "utf8"); // "Hello"
|
|
517
|
+
* ```
|
|
357
518
|
*/
|
|
358
519
|
declare function convertBytesToStr(data: Buffer, outputEncoding?: Encoding): string;
|
|
359
520
|
/**
|
|
@@ -368,6 +529,14 @@ declare function convertBytesToStr(data: Buffer, outputEncoding?: Encoding): str
|
|
|
368
529
|
* @param from - The current encoding of `data`.
|
|
369
530
|
* @param to - The target encoding for `data`.
|
|
370
531
|
* @returns A `Result` with a string or an error.
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* ```ts
|
|
535
|
+
* const {result, error, success} = tryConvertEncoding("Hello", "utf8", "base64url");
|
|
536
|
+
*
|
|
537
|
+
* if (success) console.log(result); // "..." (Base64url encoded string)
|
|
538
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
539
|
+
* ```
|
|
371
540
|
*/
|
|
372
541
|
declare function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<string>;
|
|
373
542
|
/**
|
|
@@ -383,6 +552,11 @@ declare function tryConvertEncoding(data: string, from: Encoding, to: Encoding):
|
|
|
383
552
|
* @param to - The target encoding for `data`.
|
|
384
553
|
* @returns The converted string.
|
|
385
554
|
* @throws {Error} If encodings are invalid or conversion fails.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```ts
|
|
558
|
+
* const encoded = convertEncoding("Hello", "utf8", "base64url"); // "..." (Base64url encoded string)
|
|
559
|
+
* ```
|
|
386
560
|
*/
|
|
387
561
|
declare function convertEncoding(data: string, from: Encoding, to: Encoding): string;
|
|
388
562
|
|