cipher-kit 2.1.3 → 3.0.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.
Files changed (52) hide show
  1. package/README.md +324 -434
  2. package/dist/chunk-3A4RTUKO.cjs +509 -0
  3. package/dist/chunk-3A4RTUKO.cjs.map +1 -0
  4. package/dist/chunk-7254PEID.cjs +502 -0
  5. package/dist/chunk-7254PEID.cjs.map +1 -0
  6. package/dist/chunk-GL32EZRA.js +475 -0
  7. package/dist/chunk-GL32EZRA.js.map +1 -0
  8. package/dist/chunk-IY6XGUYO.js +494 -0
  9. package/dist/chunk-IY6XGUYO.js.map +1 -0
  10. package/dist/chunk-VCBHSRCS.cjs +523 -0
  11. package/dist/chunk-VCBHSRCS.cjs.map +1 -0
  12. package/dist/chunk-X6MX4NDE.js +478 -0
  13. package/dist/chunk-X6MX4NDE.js.map +1 -0
  14. package/dist/export-B-3CCZIO.d.cts +389 -0
  15. package/dist/export-BPo6yPV-.d.ts +389 -0
  16. package/dist/export-C0_UEEg8.d.ts +396 -0
  17. package/dist/export-DPuocAr3.d.cts +396 -0
  18. package/dist/index.cjs +11 -19
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +11 -40
  21. package/dist/index.d.ts +11 -40
  22. package/dist/index.js +3 -3
  23. package/dist/index.js.map +1 -1
  24. package/dist/node.cjs +39 -35
  25. package/dist/node.d.cts +3 -3
  26. package/dist/node.d.ts +3 -3
  27. package/dist/node.js +2 -2
  28. package/dist/validate-vDTesb-X.d.cts +195 -0
  29. package/dist/validate-vDTesb-X.d.ts +195 -0
  30. package/dist/web-api.cjs +39 -35
  31. package/dist/web-api.d.cts +2 -3
  32. package/dist/web-api.d.ts +2 -3
  33. package/dist/web-api.js +2 -2
  34. package/package.json +82 -92
  35. package/dist/chunk-BMX42IZM.cjs +0 -623
  36. package/dist/chunk-BMX42IZM.cjs.map +0 -1
  37. package/dist/chunk-HTRGOBZF.cjs +0 -169
  38. package/dist/chunk-HTRGOBZF.cjs.map +0 -1
  39. package/dist/chunk-LU7QOSQH.js +0 -141
  40. package/dist/chunk-LU7QOSQH.js.map +0 -1
  41. package/dist/chunk-S6SNCTU6.js +0 -485
  42. package/dist/chunk-S6SNCTU6.js.map +0 -1
  43. package/dist/chunk-T36BEDPY.js +0 -598
  44. package/dist/chunk-T36BEDPY.js.map +0 -1
  45. package/dist/chunk-ZNM5M6RD.cjs +0 -514
  46. package/dist/chunk-ZNM5M6RD.cjs.map +0 -1
  47. package/dist/export-BaM_OTFk.d.ts +0 -573
  48. package/dist/export-CCTGAosO.d.ts +0 -572
  49. package/dist/export-FYHgb-8E.d.cts +0 -572
  50. package/dist/export-KFT0YyMg.d.cts +0 -573
  51. package/dist/validate-lkJAHCeJ.d.cts +0 -399
  52. package/dist/validate-lkJAHCeJ.d.ts +0 -399
@@ -1,399 +0,0 @@
1
- import nodeCrypto, { webcrypto } from 'node:crypto';
2
-
3
- /**
4
- * Standardized error object for the `Result` type.
5
- * Always has a brief `message` and a more detailed `description`.
6
- */
7
- interface ErrorStruct {
8
- readonly message: string;
9
- readonly description: string;
10
- }
11
- type ReservedWords<Obj extends object> = "success" extends keyof Obj ? never : "error" extends keyof Obj ? never : Obj;
12
- type OkType<T> = {
13
- readonly success: true;
14
- readonly error?: undefined;
15
- } & (T extends object ? ReservedWords<{
16
- readonly [K in keyof T]: T[K];
17
- }> : {
18
- readonly result: T;
19
- });
20
- type ErrType<T> = {
21
- readonly success: false;
22
- readonly error: ErrorStruct;
23
- } & (T extends object ? ReservedWords<{
24
- readonly [K in keyof T]?: undefined;
25
- }> : {
26
- readonly result?: undefined;
27
- });
28
- /**
29
- * Discriminated union for functions that can succeed or fail.
30
- *
31
- * - On **success**:
32
- * - If `T` is an object - properties of `T` are **spread** into the result
33
- * along with `success: true`.
34
- * - Otherwise - `{ success: true, result: T }`.
35
- * - On **failure**: `{ success: false, error: E }`.
36
- *
37
- * @example
38
- * ```ts
39
- * // Primitive result
40
- * function getNum(): Result<number> {
41
- * return $ok(42);
42
- * }
43
- * const r1 = getNum();
44
- * if (r1.success) console.log(r1.result); // 42
45
- *
46
- * // Object result (spread)
47
- * function getObject(): Result<{ name: string; age: number }> {
48
- * return $ok({ name: 'Alice', age: 30 });
49
- * }
50
- * const r2 = getObject();
51
- * if (r2.success) console.log(r2.name, r2.age); // 'Alice' 30
52
- * ```
53
- */
54
- type Result<T> = OkType<T> | ErrType<T>;
55
-
56
- declare const ENCODING: readonly ["base64", "base64url", "hex", "utf8", "latin1"];
57
- declare const CIPHER_ENCODING: readonly ["base64", "base64url", "hex"];
58
- declare const DIGEST_ALGORITHMS: Readonly<{
59
- readonly sha256: {
60
- readonly node: "sha256";
61
- readonly web: "SHA-256";
62
- };
63
- readonly sha384: {
64
- readonly node: "sha384";
65
- readonly web: "SHA-384";
66
- };
67
- readonly sha512: {
68
- readonly node: "sha512";
69
- readonly web: "SHA-512";
70
- };
71
- }>;
72
- declare const ENCRYPTION_ALGORITHMS: Readonly<{
73
- readonly aes256gcm: {
74
- readonly keyBytes: 32;
75
- readonly ivLength: 12;
76
- readonly node: "aes-256-gcm";
77
- readonly web: "AES-GCM";
78
- };
79
- readonly aes192gcm: {
80
- readonly keyBytes: 24;
81
- readonly ivLength: 12;
82
- readonly node: "aes-192-gcm";
83
- readonly web: "AES-GCM";
84
- };
85
- readonly aes128gcm: {
86
- readonly keyBytes: 16;
87
- readonly ivLength: 12;
88
- readonly node: "aes-128-gcm";
89
- readonly web: "AES-GCM";
90
- };
91
- }>;
92
-
93
- declare const __brand: unique symbol;
94
- type Brand<T> = {
95
- readonly [__brand]: T;
96
- };
97
- type SecretKey<Platform extends "web" | "node"> = {
98
- /** The platform the key is for ('web' or 'node'). */
99
- readonly platform: Platform;
100
- /** The digest algorithm used for HKDF (e.g. 'sha256'). */
101
- readonly digest: keyof typeof DIGEST_ALGORITHMS;
102
- /** The encryption algorithm used (e.g. 'aes256gcm'). */
103
- readonly algorithm: keyof typeof ENCRYPTION_ALGORITHMS;
104
- /** The actual secret key object (CryptoKey for web, KeyObject for node). */
105
- readonly key: Platform extends "web" ? webcrypto.CryptoKey : nodeCrypto.KeyObject;
106
- } & Brand<`secretKey-${Platform}`>;
107
- /** A secret key for Node.js platform. */
108
- type NodeSecretKey = SecretKey<"node">;
109
- /** A secret key for Web platform. */
110
- type WebSecretKey = SecretKey<"web">;
111
- /** Supported **cipher text** encodings for encrypted/hash outputs. */
112
- type CipherEncoding = (typeof CIPHER_ENCODING)[number];
113
- /** Supported data encodings for **plain text/bytes** conversions. */
114
- type Encoding = (typeof ENCODING)[number];
115
- /** Supported symmetric encryption algorithms */
116
- type EncryptionAlgorithm = keyof typeof ENCRYPTION_ALGORITHMS;
117
- /** Supported digest algorithms for hashing */
118
- type DigestAlgorithm = keyof typeof DIGEST_ALGORITHMS;
119
- /**
120
- * Options for creating a `SecretKey`
121
- *
122
- * ### 🍼 Explain Like I'm Five
123
- * You want to create a special key to lock your treasure box.
124
- * You can choose how strong the lock is (algorithm), how to make the key (digest),
125
- * and add some extra secret stuff (salt and info) to make your key unique.
126
- *
127
- * - `algorithm`: Encryption algorithm to use (default: `'aes256gcm'`)
128
- * - `digest`: Digest algorithm for HKDF (default: `'sha256'`)
129
- * - `salt`: Salt for HKDF (default: `'cipher-kit-salt'`, must be ≥ 8 chars)
130
- * - `info`: Added context for HKDF (default: `'cipher-kit'`)
131
- */
132
- interface CreateSecretKeyOptions {
133
- /** Encryption algorithm to use (default: `'aes256gcm'`). */
134
- algorithm?: EncryptionAlgorithm;
135
- /** Digest algorithm for HKDF (default: `'sha256'`). */
136
- digest?: DigestAlgorithm;
137
- /** Optional salt for HKDF (default: `'cipher-kit-salt'`, must be ≥ 8 characters). */
138
- salt?: string;
139
- /** Optional context info for HKDF (default: `'cipher-kit'`). */
140
- info?: string;
141
- }
142
- /**
143
- * Options for encryption.
144
- *
145
- * ### 🍼 Explain Like I'm Five
146
- * After locking your message, how should we write the locked message down?
147
- *
148
- * - `outputEncoding`: Output ciphertext encoding (`'base64' | 'base64url' | 'hex'`) (default: `'base64url'`)
149
- */
150
- interface EncryptOptions {
151
- /** Encoding format for the output ciphertext (default: `'base64url'`). */
152
- outputEncoding?: CipherEncoding;
153
- }
154
- /**
155
- * Options for decryption.
156
- *
157
- * ### 🍼 Explain Like I'm Five
158
- * To unlock the message, we must know how it was written down.
159
- *
160
- * - `inputEncoding`: Input ciphertext encoding (`'base64' | 'base64url' | 'hex'`) (default: `'base64url'`)
161
- */
162
- interface DecryptOptions {
163
- /** Encoding format for the input ciphertext (default: `'base64url'`). */
164
- inputEncoding?: CipherEncoding;
165
- }
166
- /**
167
- * Options for hashing arbitrary data.
168
- *
169
- * ### 🍼 Explain Like I'm Five
170
- * You want to create a unique fingerprint (hash) of your data.
171
- * You can choose how to create the fingerprint (digest) and how to write it down (encoding).
172
- *
173
- * - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha256'`)
174
- * - `outputEncoding`: Output ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
175
- */
176
- interface HashOptions {
177
- /** Digest algorithm to use (default: `'sha256'`). */
178
- digest?: DigestAlgorithm;
179
- /** Encoding format for the output hash (default: `'base64url'`). */
180
- outputEncoding?: CipherEncoding;
181
- }
182
- /**
183
- * Options for password hashing (PBKDF2).
184
- *
185
- * ### 🍼 Explain Like I'm Five
186
- * We turn your password into a strong secret by mixing it with salt,
187
- * stirring many times (iterations), and making a long result (keyLength).
188
- *
189
- * - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha512'`)
190
- * - `outputEncoding`: Output ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
191
- * - `saltLength`: Size of the random salt in bytes (default: `16` bytes)
192
- * - `iterations`: Number of iterations (default: `320000`)
193
- * - `keyLength`: Length of the derived key in bytes (default: `64` bytes)
194
- */
195
- interface HashPasswordOptions {
196
- /** Digest algorithm to use (default: `'sha512'`). */
197
- digest?: DigestAlgorithm;
198
- /** Encoding format for the output hash (default: `'base64url'`). */
199
- outputEncoding?: CipherEncoding;
200
- /** Length of the salt in bytes (default: `16` bytes, min: `8` bytes). */
201
- saltLength?: number;
202
- /** Number of iterations for key derivation (default: `320000`, min: `1000`). */
203
- iterations?: number;
204
- /** Length of the derived key in bytes (default: `64` bytes, min: `16` bytes). */
205
- keyLength?: number;
206
- }
207
- /**
208
- * Options for verifying a password hash (PBKDF2) (must match the parameters used to hash).
209
- *
210
- * ### 🍼 Explain Like I'm Five
211
- * To check a password, we must use the same recipe—same mixer, same number of stirs,
212
- * same size—so the new result matches the old one.
213
- *
214
- * - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha512'`)
215
- * - `inputEncoding`: Input ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
216
- * - `iterations`: Number of iterations (default: `320000`)
217
- * - `keyLength`: Length of the derived key in bytes (default: `64` bytes)
218
- */
219
- interface VerifyPasswordOptions {
220
- /** Digest algorithm to use (default: `'sha512'`). */
221
- digest?: DigestAlgorithm;
222
- /** Encoding format of the input hash (default: `'base64url'`). */
223
- inputEncoding?: CipherEncoding;
224
- /** Number of iterations for key derivation (default: `320000`). */
225
- iterations?: number;
226
- /** Length of the derived key in bytes (default: `64` bytes). */
227
- keyLength?: number;
228
- }
229
-
230
- /**
231
- * Safely serializes a plain object to JSON without throwing.
232
- *
233
- * Wraps `JSON.stringify` and returns a `Result` containing the JSON string or an error.
234
- * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.
235
- *
236
- * ### 🍼 Explain Like I'm Five
237
- * You have a box of toys (your object) and take a photo of it (a JSON string)
238
- * so you can send it to a friend.
239
- *
240
- * @template T - Plain object type to serialize.
241
- * @param obj - The object to stringify (must be a plain object).
242
- * @returns A `Result` with the JSON string on success, or an error.
243
- *
244
- * @example
245
- * ```ts
246
- * const { result, error, success } = tryStringifyObj({ a: 1 });
247
- *
248
- * if (success) console.log(result); // "{\"a\":1}"
249
- * else console.error(error); // { message: "...", description: "..." }
250
- * ```
251
- */
252
- declare function tryStringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string>;
253
- /**
254
- * Serializes a plain object to JSON (throwing).
255
- *
256
- * Wraps `JSON.stringify` and returns the result or throws an error.
257
- * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.
258
- *
259
- * ### 🍼 Explain Like I'm Five
260
- * You have a box of toys (your object) and take a photo of it (a JSON string)
261
- * so you can send it to a friend.
262
- *
263
- * @template T - Plain object type to serialize.
264
- * @param obj - The object to stringify (must be a plain object).
265
- * @returns JSON string representation of the object.
266
- * @throws {Error} If `obj` is not a plain object or serialization fails.
267
- *
268
- * @example
269
- * ```ts
270
- * const json = stringifyObj({ a: 1 }); // "{\"a\":1}"
271
- * ```
272
- */
273
- declare function stringifyObj<T extends object = Record<string, unknown>>(obj: T): string;
274
- /**
275
- * Safely parses a JSON string to a plain object (non-throwing).
276
- *
277
- * Wraps `JSON.parse` and returns a `Result` containing the parsed object, or an error.
278
- *
279
- * ### 🍼 Explain Like I'm Five
280
- * You rebuild your toy box (an object) from a photo you took (a JSON string).
281
- *
282
- * @template T - The expected object type.
283
- * @param str - The JSON string to parse.
284
- * @returns A `Result` with the parsed object on success, or an error.
285
- *
286
- * @example
287
- * ```ts
288
- * const {result, error, success} = tryParseToObj<{ a: number }>('{"a":1}');
289
- *
290
- * if (success) console.log(result); // { a: 1 }
291
- * else console.error(error) // { message: "...", description: "..." }
292
- * ```
293
- */
294
- declare function tryParseToObj<T extends object = Record<string, unknown>>(str: string): Result<{
295
- result: T;
296
- }>;
297
- /**
298
- * Parses a JSON string to a plain object (throwing).
299
- *
300
- * Wraps `JSON.parse` and returns the parsed object, or throws on failure.
301
- *
302
- * ### 🍼 Explain Like I'm Five
303
- * You rebuild your toy box (an object) from a photo you took (a JSON string).
304
- *
305
- * @template T - The expected object type.
306
- * @param str - The JSON string to parse.
307
- * @returns The parsed plain object.
308
- * @throws {Error} If the string can’t be parsed or doesn’t represent a plain object.
309
- *
310
- * @example
311
- * ```ts
312
- * const obj = parseToObj<{ a: number }>('{"a":1}'); // obj.a === 1
313
- * ```
314
- */
315
- declare function parseToObj<T extends object = Record<string, unknown>>(str: string): T;
316
-
317
- /**
318
- * Type guard to check if the provided value is a SecretKey object for Node.js environment.
319
- *
320
- * ### 🍼 Explain Like I'm Five
321
- * Checking if the key in your hand is the right kind of key for the lock.
322
- *
323
- * @param x - The value to check.
324
- * @returns True if the value is a SecretKey object for Node.js, false otherwise.
325
- *
326
- * @example
327
- * ```ts
328
- * isNodeSecretKey(nodeKey); // true
329
- * isNodeSecretKey(webKey); // false
330
- * isNodeSecretKey({}); // false
331
- * ```
332
- */
333
- declare function isNodeSecretKey(x: unknown): x is NodeSecretKey;
334
- /**
335
- * Type guard to check if the provided value is a SecretKey object for Web (Deno, Bun, Cloudflare included) environment.
336
- *
337
- * ### 🍼 Explain Like I'm Five
338
- * Checking if the key in your hand is the right kind of key for the lock.
339
- *
340
- * @param x - The value to check.
341
- * @returns True if the value is a SecretKey object for Web, false otherwise.
342
- *
343
- * @example
344
- * ```ts
345
- * isWebSecretKey(webKey); // true
346
- * isWebSecretKey(nodeKey); // false
347
- * isWebSecretKey({}); // false
348
- * ```
349
- */
350
- declare function isWebSecretKey(x: unknown): x is WebSecretKey;
351
- /**
352
- * Regular expressions for encrypted data patterns.
353
- *
354
- * - **node**: `"iv.cipher.tag."` (three dot-separated parts plus a trailing dot)
355
- * - **web**: `"iv.cipherWithTag."` (two parts plus a trailing dot)
356
- * - **general**: accepts both shapes (2 or 3 parts) with a trailing dot
357
- *
358
- * Each part is any non-empty string without dots.
359
- *
360
- * ### 🍼 Explain Like I'm Five
361
- * You have a secret code you want to check. Before you check it,
362
- * you make sure it looks how a secret code should look.
363
- */
364
- declare const ENCRYPTED_REGEX: Readonly<{
365
- node: RegExp;
366
- web: RegExp;
367
- general: RegExp;
368
- }>;
369
- /**
370
- * Checks if a string matches an expected encrypted payload shape.
371
- *
372
- * - **node**: `"iv.cipher.tag."` (three dot-separated parts plus a trailing dot)
373
- * - **web**: `"iv.cipherWithTag."` (two parts plus a trailing dot)
374
- * - **general**: accepts both shapes (2 or 3 parts) with a trailing dot
375
- *
376
- * Each part is any non-empty string without dots.
377
- *
378
- * This validates only the **shape**, not whether content is valid base64/hex, etc.
379
- *
380
- * ### 🍼 Explain Like I'm Five
381
- * You have a secret code you want to check. Before you check it,
382
- * you make sure it looks how a secret code should look.
383
- *
384
- * @param data - The string to test.
385
- * @param format - Which layout to check: `'node'`, `'web'`, or `'general'`.
386
- * @returns `true` if the string matches the pattern; otherwise `false`.
387
- * @throws {Error} If an unknown `format` is provided.
388
- *
389
- * @example
390
- * ```ts
391
- * matchEncryptedPattern("abc.def.ghi.", "node"); // true
392
- * matchEncryptedPattern("abc.def.", "web"); // true
393
- * matchEncryptedPattern("abc.def.", "node"); // false
394
- * matchEncryptedPattern("abc.def.ghi.", "general"); // true
395
- * ```
396
- */
397
- declare function matchEncryptedPattern(data: string, format: "node" | "web" | "general"): boolean;
398
-
399
- export { type CipherEncoding as C, type DigestAlgorithm as D, ENCRYPTED_REGEX as E, type HashOptions as H, type NodeSecretKey as N, type Result as R, type VerifyPasswordOptions as V, type WebSecretKey as W, tryStringifyObj as a, isWebSecretKey as b, type Encoding as c, type EncryptionAlgorithm as d, type CreateSecretKeyOptions as e, type EncryptOptions as f, type DecryptOptions as g, type HashPasswordOptions as h, isNodeSecretKey as i, matchEncryptedPattern as m, parseToObj as p, stringifyObj as s, tryParseToObj as t };