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