@venizia/ignis-docs 0.0.5 → 0.0.6-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.
Files changed (123) hide show
  1. package/package.json +1 -1
  2. package/wiki/best-practices/architectural-patterns.md +0 -2
  3. package/wiki/best-practices/architecture-decisions.md +0 -8
  4. package/wiki/best-practices/code-style-standards/control-flow.md +1 -1
  5. package/wiki/best-practices/code-style-standards/index.md +0 -1
  6. package/wiki/best-practices/code-style-standards/tooling.md +0 -3
  7. package/wiki/best-practices/contribution-workflow.md +12 -12
  8. package/wiki/best-practices/index.md +4 -14
  9. package/wiki/best-practices/performance-optimization.md +3 -3
  10. package/wiki/best-practices/security-guidelines.md +2 -2
  11. package/wiki/best-practices/troubleshooting-tips.md +1 -1
  12. package/wiki/guides/core-concepts/application/bootstrapping.md +6 -7
  13. package/wiki/guides/core-concepts/components-guide.md +1 -1
  14. package/wiki/guides/core-concepts/components.md +2 -2
  15. package/wiki/guides/core-concepts/dependency-injection.md +4 -5
  16. package/wiki/guides/core-concepts/persistent/datasources.md +4 -5
  17. package/wiki/guides/core-concepts/services.md +1 -1
  18. package/wiki/guides/get-started/5-minute-quickstart.md +4 -5
  19. package/wiki/guides/get-started/philosophy.md +12 -24
  20. package/wiki/guides/index.md +2 -9
  21. package/wiki/guides/reference/mcp-docs-server.md +13 -13
  22. package/wiki/guides/tutorials/building-a-crud-api.md +10 -10
  23. package/wiki/guides/tutorials/complete-installation.md +11 -12
  24. package/wiki/guides/tutorials/ecommerce-api.md +3 -3
  25. package/wiki/guides/tutorials/realtime-chat.md +6 -6
  26. package/wiki/guides/tutorials/testing.md +4 -5
  27. package/wiki/index.md +8 -14
  28. package/wiki/references/base/bootstrapping.md +0 -3
  29. package/wiki/references/base/components.md +2 -2
  30. package/wiki/references/base/controllers.md +0 -1
  31. package/wiki/references/base/datasources.md +1 -1
  32. package/wiki/references/base/dependency-injection.md +2 -2
  33. package/wiki/references/base/filter-system/default-filter.md +2 -3
  34. package/wiki/references/base/filter-system/index.md +1 -1
  35. package/wiki/references/base/filter-system/quick-reference.md +0 -14
  36. package/wiki/references/base/middlewares.md +0 -8
  37. package/wiki/references/base/providers.md +0 -9
  38. package/wiki/references/base/repositories/advanced.md +1 -1
  39. package/wiki/references/base/repositories/mixins.md +2 -3
  40. package/wiki/references/base/services.md +0 -1
  41. package/wiki/references/components/authentication/api.md +444 -0
  42. package/wiki/references/components/authentication/errors.md +177 -0
  43. package/wiki/references/components/authentication/index.md +571 -0
  44. package/wiki/references/components/authentication/usage.md +781 -0
  45. package/wiki/references/components/health-check.md +292 -103
  46. package/wiki/references/components/index.md +14 -12
  47. package/wiki/references/components/mail/api.md +505 -0
  48. package/wiki/references/components/mail/errors.md +176 -0
  49. package/wiki/references/components/mail/index.md +535 -0
  50. package/wiki/references/components/mail/usage.md +404 -0
  51. package/wiki/references/components/request-tracker.md +229 -25
  52. package/wiki/references/components/socket-io/api.md +1051 -0
  53. package/wiki/references/components/socket-io/errors.md +119 -0
  54. package/wiki/references/components/socket-io/index.md +410 -0
  55. package/wiki/references/components/socket-io/usage.md +322 -0
  56. package/wiki/references/components/static-asset/api.md +261 -0
  57. package/wiki/references/components/static-asset/errors.md +89 -0
  58. package/wiki/references/components/static-asset/index.md +617 -0
  59. package/wiki/references/components/static-asset/usage.md +364 -0
  60. package/wiki/references/components/swagger.md +390 -110
  61. package/wiki/references/components/template/api-page.md +125 -0
  62. package/wiki/references/components/template/errors-page.md +100 -0
  63. package/wiki/references/components/template/index.md +104 -0
  64. package/wiki/references/components/template/setup-page.md +134 -0
  65. package/wiki/references/components/template/single-page.md +132 -0
  66. package/wiki/references/components/template/usage-page.md +127 -0
  67. package/wiki/references/components/websocket/api.md +508 -0
  68. package/wiki/references/components/websocket/errors.md +123 -0
  69. package/wiki/references/components/websocket/index.md +453 -0
  70. package/wiki/references/components/websocket/usage.md +475 -0
  71. package/wiki/references/helpers/cron/index.md +224 -0
  72. package/wiki/references/helpers/crypto/index.md +537 -0
  73. package/wiki/references/helpers/env/index.md +214 -0
  74. package/wiki/references/helpers/error/index.md +232 -0
  75. package/wiki/references/helpers/index.md +16 -15
  76. package/wiki/references/helpers/inversion/index.md +608 -0
  77. package/wiki/references/helpers/logger/index.md +600 -0
  78. package/wiki/references/helpers/network/api.md +986 -0
  79. package/wiki/references/helpers/network/index.md +620 -0
  80. package/wiki/references/helpers/queue/index.md +589 -0
  81. package/wiki/references/helpers/redis/index.md +495 -0
  82. package/wiki/references/helpers/socket-io/api.md +497 -0
  83. package/wiki/references/helpers/socket-io/index.md +513 -0
  84. package/wiki/references/helpers/storage/api.md +705 -0
  85. package/wiki/references/helpers/storage/index.md +583 -0
  86. package/wiki/references/helpers/template/index.md +66 -0
  87. package/wiki/references/helpers/template/single-page.md +126 -0
  88. package/wiki/references/helpers/testing/index.md +510 -0
  89. package/wiki/references/helpers/types/index.md +512 -0
  90. package/wiki/references/helpers/uid/index.md +272 -0
  91. package/wiki/references/helpers/websocket/api.md +736 -0
  92. package/wiki/references/helpers/websocket/index.md +574 -0
  93. package/wiki/references/helpers/worker-thread/index.md +470 -0
  94. package/wiki/references/index.md +2 -9
  95. package/wiki/references/quick-reference.md +3 -18
  96. package/wiki/references/utilities/jsx.md +1 -8
  97. package/wiki/references/utilities/statuses.md +0 -7
  98. package/wiki/references/components/authentication.md +0 -476
  99. package/wiki/references/components/mail.md +0 -687
  100. package/wiki/references/components/socket-io.md +0 -562
  101. package/wiki/references/components/static-asset.md +0 -1277
  102. package/wiki/references/helpers/cron.md +0 -108
  103. package/wiki/references/helpers/crypto.md +0 -132
  104. package/wiki/references/helpers/env.md +0 -83
  105. package/wiki/references/helpers/error.md +0 -97
  106. package/wiki/references/helpers/inversion.md +0 -176
  107. package/wiki/references/helpers/logger.md +0 -296
  108. package/wiki/references/helpers/network.md +0 -396
  109. package/wiki/references/helpers/queue.md +0 -150
  110. package/wiki/references/helpers/redis.md +0 -142
  111. package/wiki/references/helpers/socket-io.md +0 -932
  112. package/wiki/references/helpers/storage.md +0 -665
  113. package/wiki/references/helpers/testing.md +0 -133
  114. package/wiki/references/helpers/types.md +0 -167
  115. package/wiki/references/helpers/uid.md +0 -167
  116. package/wiki/references/helpers/worker-thread.md +0 -178
  117. package/wiki/references/src-details/boot.md +0 -379
  118. package/wiki/references/src-details/core.md +0 -263
  119. package/wiki/references/src-details/dev-configs.md +0 -298
  120. package/wiki/references/src-details/docs.md +0 -71
  121. package/wiki/references/src-details/helpers.md +0 -211
  122. package/wiki/references/src-details/index.md +0 -86
  123. package/wiki/references/src-details/inversion.md +0 -340
@@ -0,0 +1,537 @@
1
+ # Crypto
2
+
3
+ Cryptographic utilities for AES symmetric encryption, RSA asymmetric encryption, ECDH key exchange, and hashing.
4
+
5
+ ## Quick Reference
6
+
7
+ | Class | Extends | Use Case |
8
+ |-------|---------|----------|
9
+ | **AES** | BaseCryptoAlgorithm | Fast symmetric encryption (AES-256-CBC, AES-256-GCM) |
10
+ | **RSA** | BaseCryptoAlgorithm | Public-key encryption with DER key pairs |
11
+ | **ECDH** | AbstractCryptoAlgorithm | Ephemeral key exchange with AES-256-GCM session encryption |
12
+ | **hash()** | _(standalone function)_ | MD5 and SHA256 HMAC hashing |
13
+
14
+ #### Algorithm Comparison
15
+
16
+ | Feature | AES | RSA | ECDH |
17
+ |---------|-----|-----|------|
18
+ | Type | Symmetric | Asymmetric | Asymmetric + Symmetric |
19
+ | Key exchange | Shared secret | Public/private | Diffie-Hellman |
20
+ | Speed | Fast | Slow (large keys) | Fast (small keys) |
21
+ | Max message | Unlimited | ~190 bytes (2048-bit) | Unlimited |
22
+ | Async | No | No | Yes (Web Crypto) |
23
+ | Runtime | Node.js `crypto` | Node.js `crypto` | `crypto.subtle` (Bun/Browser) |
24
+
25
+ #### Import Paths
26
+
27
+ ```typescript
28
+ // Algorithm classes
29
+ import { AES, RSA, ECDH } from '@venizia/ignis-helpers';
30
+
31
+ // Hash utility function
32
+ import { hash } from '@venizia/ignis-helpers';
33
+
34
+ // Types
35
+ import type {
36
+ AESAlgorithmType,
37
+ RSAAlgorithmType,
38
+ ECDHAlgorithmType,
39
+ IECDHEncryptedPayload,
40
+ IECDHExtraOptions,
41
+ ICryptoAlgorithm,
42
+ } from '@venizia/ignis-helpers';
43
+ ```
44
+
45
+ #### Type Hierarchy
46
+
47
+ All crypto algorithms share a common type hierarchy with 7 generic type parameters:
48
+
49
+ ```
50
+ ICryptoAlgorithm (interface)
51
+ └── AbstractCryptoAlgorithm (extends BaseHelper)
52
+ ├── BaseCryptoAlgorithm (adds normalizeSecretKey, getAlgorithmKeySize)
53
+ │ ├── AES
54
+ │ └── RSA
55
+ └── ECDH (uses CryptoKey objects, not string secrets)
56
+ ```
57
+
58
+ **Why two base classes?**
59
+ - `BaseCryptoAlgorithm` adds `normalizeSecretKey()` and `getAlgorithmKeySize()` -- useful for AES/RSA which use string secrets with size normalization
60
+ - `ECDH` extends `AbstractCryptoAlgorithm` directly because it uses `CryptoKey` objects (Web Crypto), not string secrets
61
+
62
+ ```typescript
63
+ interface ICryptoAlgorithm<
64
+ AlgorithmNameType extends string,
65
+ EncryptInputType = unknown,
66
+ DecryptInputType = unknown,
67
+ SecretKeyType = unknown,
68
+ EncryptReturnType = unknown,
69
+ DecryptReturnType = unknown,
70
+ ExtraOptions = unknown,
71
+ > {
72
+ algorithm: AlgorithmNameType;
73
+ encrypt(opts: { message: EncryptInputType; secret: SecretKeyType; opts?: ExtraOptions }): EncryptReturnType;
74
+ decrypt(opts: { message: DecryptInputType; secret: SecretKeyType; opts?: ExtraOptions }): DecryptReturnType;
75
+ }
76
+ ```
77
+
78
+ ## Creating an Instance
79
+
80
+ All crypto algorithm classes extend `BaseHelper` (via `AbstractCryptoAlgorithm`), providing scoped logging. Each class uses a static `withAlgorithm()` factory method.
81
+
82
+ ```typescript
83
+ import { AES, RSA, ECDH } from '@venizia/ignis-helpers';
84
+
85
+ // AES -- choose CBC or GCM mode
86
+ const aesCbc = AES.withAlgorithm('aes-256-cbc');
87
+ const aesGcm = AES.withAlgorithm('aes-256-gcm');
88
+
89
+ // RSA -- single algorithm, no parameters
90
+ const rsa = RSA.withAlgorithm();
91
+
92
+ // ECDH -- optional HKDF info for key isolation
93
+ const ecdh = ECDH.withAlgorithm();
94
+ const ecdhCustom = ECDH.withAlgorithm({
95
+ algorithm: 'ecdh-p256',
96
+ hkdfInfo: 'my-app-session-keys',
97
+ });
98
+ ```
99
+
100
+ #### AES Constructor Options
101
+
102
+ | Algorithm | Mode | Features |
103
+ |-----------|------|----------|
104
+ | `aes-256-cbc` | CBC | Standard block cipher, widely compatible |
105
+ | `aes-256-gcm` | GCM | Authenticated encryption -- detects tampering |
106
+
107
+ #### ECDH Constructor Options
108
+
109
+ | Option | Type | Default | Description |
110
+ |--------|------|---------|-------------|
111
+ | `algorithm` | `'ecdh-p256'` | `'ecdh-p256'` | Curve algorithm |
112
+ | `hkdfInfo` | `string` | `'ignis-ecdh-p256-aes-256-gcm-v1'` | HKDF info string for key derivation isolation |
113
+
114
+ Different `hkdfInfo` values produce **incompatible keys** from the same ECDH shared secret. Use this to isolate key derivation between different application contexts.
115
+
116
+ ## Usage
117
+
118
+ ### AES Encryption
119
+
120
+ The `AES` class provides encryption and decryption using the Advanced Encryption Standard with 256-bit keys.
121
+
122
+ ```typescript
123
+ const aes = AES.withAlgorithm('aes-256-gcm');
124
+ const secret = 'my-application-secret-key';
125
+
126
+ // Encrypt
127
+ const encrypted = aes.encrypt({ message: 'This is a secret message.', secret });
128
+ // => base64 encoded string containing IV + ciphertext
129
+
130
+ // Decrypt
131
+ const decrypted = aes.decrypt({ message: encrypted, secret });
132
+ // => 'This is a secret message.'
133
+ ```
134
+
135
+ > [!TIP]
136
+ > Prefer `aes-256-gcm` for new applications. It provides **authenticated encryption** -- if the ciphertext is tampered with, decryption will throw an error rather than silently returning corrupted data. This does not happen with CBC mode.
137
+
138
+ #### AES Extra Options
139
+
140
+ ```typescript
141
+ import C from 'node:crypto';
142
+
143
+ const encrypted = aes.encrypt({
144
+ message: 'hello',
145
+ secret: 'my-secret',
146
+ opts: {
147
+ iv: C.randomBytes(16), // Custom IV (default: random 16 bytes)
148
+ inputEncoding: 'utf-8', // Message input encoding (default: 'utf-8')
149
+ outputEncoding: 'hex', // Ciphertext output encoding (default: 'base64')
150
+ doThrow: false, // Return original message on error (default: true)
151
+ },
152
+ });
153
+ ```
154
+
155
+ | Option | Type | Default (encrypt) | Default (decrypt) | Description |
156
+ |--------|------|-------------------|-------------------|-------------|
157
+ | `iv` | `Buffer` | `crypto.randomBytes(16)` | Extracted from ciphertext | Initialization vector |
158
+ | `inputEncoding` | `crypto.Encoding` | `'utf-8'` | `'base64'` | Encoding of the input message |
159
+ | `outputEncoding` | `crypto.Encoding` | `'base64'` | `'utf-8'` | Encoding of the output |
160
+ | `doThrow` | `boolean` | `true` | `true` | If `false`, returns the original message on error instead of throwing |
161
+
162
+ #### File Encryption
163
+
164
+ ```typescript
165
+ // Encrypt file contents -> returns encrypted string
166
+ const encrypted = aes.encryptFile({
167
+ absolutePath: '/path/to/config.json',
168
+ secret: 'my-secret',
169
+ });
170
+
171
+ // Decrypt file contents -> returns decrypted string
172
+ const decrypted = aes.decryptFile({
173
+ absolutePath: '/path/to/config.json.enc',
174
+ secret: 'my-secret',
175
+ });
176
+ ```
177
+
178
+ Both methods read the file synchronously via `fs.readFileSync`, convert to UTF-8, then encrypt/decrypt as a string. If `absolutePath` is empty or falsy, they return an empty string.
179
+
180
+ ### RSA Encryption
181
+
182
+ The `RSA` class provides public-key encryption using RSA with DER-formatted keys.
183
+
184
+ #### Generating a Key Pair
185
+
186
+ Keys are generated in DER format (binary, compact).
187
+
188
+ ```typescript
189
+ const rsa = RSA.withAlgorithm();
190
+
191
+ // Default: 2048-bit modulus
192
+ const { publicKey, privateKey } = rsa.generateDERKeyPair();
193
+
194
+ // Custom modulus length
195
+ const keys = rsa.generateDERKeyPair({ modulus: 4096 });
196
+ ```
197
+
198
+ | Option | Type | Default | Description |
199
+ |--------|------|---------|-------------|
200
+ | `modulus` | `number` | `2048` | RSA modulus length in bits |
201
+
202
+ The returned `publicKey` is a `Buffer` in SPKI/DER format and `privateKey` is a `Buffer` in PKCS8/DER format.
203
+
204
+ #### Encrypting and Decrypting
205
+
206
+ ```typescript
207
+ // Encrypt using the public key (base64-encoded DER)
208
+ const pubKeyB64 = publicKey.toString('base64');
209
+ const encrypted = rsa.encrypt({ message: 'secret data', secret: pubKeyB64 });
210
+
211
+ // Decrypt using the private key (base64-encoded DER)
212
+ const privKeyB64 = privateKey.toString('base64');
213
+ const decrypted = rsa.decrypt({ message: encrypted, secret: privKeyB64 });
214
+ // => 'secret data'
215
+ ```
216
+
217
+ #### RSA Extra Options
218
+
219
+ ```typescript
220
+ const encrypted = rsa.encrypt({
221
+ message: 'hello',
222
+ secret: pubKeyB64,
223
+ opts: {
224
+ inputEncoding: {
225
+ key: 'base64', // Key encoding (default: 'base64')
226
+ message: 'utf-8', // Message encoding (default: 'utf-8')
227
+ },
228
+ outputEncoding: 'hex', // Ciphertext output (default: 'base64')
229
+ doThrow: false, // Return original on error (default: true)
230
+ },
231
+ });
232
+ ```
233
+
234
+ | Option | Type | Default (encrypt) | Default (decrypt) | Description |
235
+ |--------|------|-------------------|-------------------|-------------|
236
+ | `inputEncoding.key` | `crypto.Encoding` | `'base64'` | `'base64'` | Encoding of the key buffer |
237
+ | `inputEncoding.message` | `crypto.Encoding` | `'utf-8'` | `'base64'` | Encoding of the input message |
238
+ | `outputEncoding` | `crypto.Encoding` | `'base64'` | `'utf-8'` | Encoding of the output |
239
+ | `doThrow` | `boolean` | `true` | `true` | If `false`, returns the original message on error instead of throwing |
240
+
241
+ #### Error Handling
242
+
243
+ ```typescript
244
+ // Default: throws on invalid key
245
+ try {
246
+ rsa.encrypt({ message: 'test', secret: 'invalid-key' });
247
+ } catch (error) {
248
+ // Handle encryption error
249
+ }
250
+
251
+ // Graceful: return original message on error
252
+ const result = rsa.encrypt({
253
+ message: 'test',
254
+ secret: 'invalid-key',
255
+ opts: { doThrow: false },
256
+ });
257
+ // result === 'test' (original message returned)
258
+ ```
259
+
260
+ ### ECDH Key Exchange
261
+
262
+ The `ECDH` class provides **ephemeral key exchange** using ECDH P-256 with HKDF-derived AES-256-GCM session encryption. It uses the Web Crypto API (`crypto.subtle`) and is fully async.
263
+
264
+ #### When to Use ECDH
265
+
266
+ | Scenario | Use ECDH | Use AES/RSA |
267
+ |----------|----------|-------------|
268
+ | Two parties need a shared secret without pre-sharing | Yes | No |
269
+ | WebSocket session encryption | Yes | No |
270
+ | Encrypting data at rest | No | AES |
271
+ | Signing/verifying tokens | No | RSA |
272
+ | Forward secrecy needed | Yes | No |
273
+
274
+ #### Complete Key Exchange Flow
275
+
276
+ ```typescript
277
+ const ecdh = ECDH.withAlgorithm();
278
+
279
+ // 1. Both parties generate key pairs
280
+ const alice = await ecdh.generateKeyPair();
281
+ const bob = await ecdh.generateKeyPair();
282
+
283
+ // 2. Exchange public keys (over any channel -- they're safe to share)
284
+ const alicePubForBob = await ecdh.importPublicKey({ rawKeyB64: alice.publicKeyB64 });
285
+ const bobPubForAlice = await ecdh.importPublicKey({ rawKeyB64: bob.publicKeyB64 });
286
+
287
+ // 3. Initiator derives AES key (generates a random salt)
288
+ const { key: aliceKey, salt } = await ecdh.deriveAESKey({
289
+ privateKey: alice.keyPair.privateKey,
290
+ peerPublicKey: bobPubForAlice,
291
+ });
292
+
293
+ // 4. Responder derives the SAME AES key using the initiator's salt
294
+ const { key: bobKey } = await ecdh.deriveAESKey({
295
+ privateKey: bob.keyPair.privateKey,
296
+ peerPublicKey: alicePubForBob,
297
+ salt, // Must use the same salt for keys to match
298
+ });
299
+
300
+ // 5. Alice encrypts -> Bob decrypts (or vice versa)
301
+ const encrypted = await ecdh.encrypt({ message: 'Hello Bob!', secret: aliceKey });
302
+ const decrypted = await ecdh.decrypt({ message: encrypted, secret: bobKey });
303
+ // => 'Hello Bob!'
304
+ ```
305
+
306
+ > [!IMPORTANT]
307
+ > Both parties **must** use the same salt for `deriveAESKey` to produce matching keys. The initiator omits the `salt` parameter (a random 32-byte salt is generated), then shares the returned `salt` string with the responder.
308
+
309
+ #### Key Generation and Import
310
+
311
+ ```typescript
312
+ // Generate a key pair
313
+ const { keyPair, publicKeyB64 } = await ecdh.generateKeyPair();
314
+ // keyPair.publicKey -- CryptoKey (exported as raw base64 via publicKeyB64)
315
+ // keyPair.privateKey -- CryptoKey (non-extractable)
316
+ // publicKeyB64 -- base64 encoded raw public key (65 bytes for P-256)
317
+
318
+ // Import a peer's base64-encoded public key
319
+ const peerKey = await ecdh.importPublicKey({ rawKeyB64: peerPublicKeyB64 });
320
+ ```
321
+
322
+ #### AES Key Derivation
323
+
324
+ The derived key uses **HKDF** (HMAC-based Key Derivation Function) with SHA-256 to produce an AES-256-GCM key from the ECDH shared secret. A random 32-byte salt is generated if not provided.
325
+
326
+ ```typescript
327
+ // Initiator: omit salt (random salt is generated)
328
+ const { key: aesKey, salt } = await ecdh.deriveAESKey({
329
+ privateKey: myKeyPair.privateKey,
330
+ peerPublicKey: importedPeerPublicKey,
331
+ });
332
+ // aesKey -- CryptoKey for AES-256-GCM (non-extractable, encrypt + decrypt)
333
+ // salt -- base64 encoded 32-byte salt (share with peer)
334
+
335
+ // Responder: provide the initiator's salt
336
+ const { key: peerAesKey } = await ecdh.deriveAESKey({
337
+ privateKey: peerKeyPair.privateKey,
338
+ peerPublicKey: importedMyPublicKey,
339
+ salt, // Same salt -> same derived key
340
+ });
341
+ ```
342
+
343
+ #### `deriveAESKey` Options
344
+
345
+ | Option | Type | Default | Description |
346
+ |--------|------|---------|-------------|
347
+ | `privateKey` | `CryptoKey` | -- | Your ECDH private key from `generateKeyPair()` |
348
+ | `peerPublicKey` | `CryptoKey` | -- | Peer's public key from `importPublicKey()` |
349
+ | `salt` | `string` | Random 32 bytes | Base64-encoded salt for HKDF. Omit to generate a new random salt. |
350
+
351
+ #### Additional Authenticated Data (AAD)
352
+
353
+ ECDH encrypt/decrypt supports **Additional Authenticated Data** via the `opts.additionalData` parameter. AAD is authenticated but not encrypted -- it binds the ciphertext to a context (e.g., channel ID, session ID) so the same ciphertext cannot be replayed in a different context.
354
+
355
+ ```typescript
356
+ // Encrypt with AAD
357
+ const encrypted = await ecdh.encrypt({
358
+ message: 'context-bound message',
359
+ secret: sharedKey,
360
+ opts: { additionalData: 'channel-123' },
361
+ });
362
+
363
+ // Decrypt must provide the SAME AAD
364
+ const decrypted = await ecdh.decrypt({
365
+ message: encrypted,
366
+ secret: sharedKey,
367
+ opts: { additionalData: 'channel-123' },
368
+ });
369
+
370
+ // Decrypt with wrong/missing AAD throws
371
+ await ecdh.decrypt({ message: encrypted, secret: sharedKey });
372
+ // => throws (AAD mismatch)
373
+ ```
374
+
375
+ #### Encrypted Payload Format
376
+
377
+ ```typescript
378
+ interface IECDHEncryptedPayload {
379
+ iv: string; // base64 encoded 12-byte IV
380
+ ct: string; // base64 encoded ciphertext + GCM auth tag (128-bit)
381
+ }
382
+ ```
383
+
384
+ #### Security Properties
385
+
386
+ | Property | Guarantee |
387
+ |----------|-----------|
388
+ | **Confidentiality** | AES-256-GCM encryption |
389
+ | **Integrity** | GCM authentication tag -- tampered ciphertext is detected |
390
+ | **Forward secrecy** | Ephemeral key pairs -- compromising one session doesn't compromise others |
391
+ | **Key isolation** | HKDF info parameter separates key derivation contexts |
392
+ | **Context binding** | AAD (`additionalData`) prevents cross-context replay |
393
+
394
+ ### Hashing
395
+
396
+ Standalone `hash` utility function for creating hashes (e.g., for data integrity checks or HMAC signatures).
397
+
398
+ ```typescript
399
+ import { hash } from '@venizia/ignis-helpers';
400
+
401
+ // MD5 Hash
402
+ const md5Hash = hash('some text', { algorithm: 'MD5', outputType: 'hex' });
403
+
404
+ // SHA256 HMAC (secret is required for SHA256)
405
+ const sha256Hash = hash('some text', {
406
+ algorithm: 'SHA256',
407
+ secret: 'a-secret-key',
408
+ outputType: 'hex',
409
+ });
410
+ ```
411
+
412
+ > [!WARNING]
413
+ > `SHA256` mode uses HMAC and **requires** the `secret` parameter. If `secret` is omitted, the function returns the original text unchanged (no hash is computed). `MD5` mode does not use a secret.
414
+
415
+ #### `hash` Function Signature
416
+
417
+ ```typescript
418
+ function hash(
419
+ text: string,
420
+ options: {
421
+ algorithm: 'SHA256' | 'MD5';
422
+ secret?: string;
423
+ outputType: C.BinaryToTextEncoding; // 'hex' | 'base64' | 'base64url'
424
+ },
425
+ ): string;
426
+ ```
427
+
428
+ | Option | Type | Required | Description |
429
+ |--------|------|----------|-------------|
430
+ | `algorithm` | `'SHA256' \| 'MD5'` | Yes | Hashing algorithm to use |
431
+ | `secret` | `string` | Only for SHA256 | HMAC secret key. Required for SHA256, ignored for MD5. |
432
+ | `outputType` | `'hex' \| 'base64' \| 'base64url'` | Yes | Output encoding of the hash digest |
433
+
434
+ ## API Summary
435
+
436
+ | Method | Class | Returns | Description |
437
+ |--------|-------|---------|-------------|
438
+ | `AES.withAlgorithm(algorithm)` | AES | `AES` | Create AES instance with CBC or GCM mode |
439
+ | `encrypt(opts)` | AES | `string` | Encrypt a string message |
440
+ | `decrypt(opts)` | AES | `string` | Decrypt a ciphertext string |
441
+ | `encryptFile(opts)` | AES | `string` | Encrypt file contents to string |
442
+ | `decryptFile(opts)` | AES | `string` | Decrypt file contents to string |
443
+ | `RSA.withAlgorithm()` | RSA | `RSA` | Create RSA instance |
444
+ | `generateDERKeyPair(opts?)` | RSA | `{ publicKey: Buffer, privateKey: Buffer }` | Generate DER-format key pair |
445
+ | `encrypt(opts)` | RSA | `string` | Encrypt with public key |
446
+ | `decrypt(opts)` | RSA | `string` | Decrypt with private key |
447
+ | `ECDH.withAlgorithm(opts?)` | ECDH | `ECDH` | Create ECDH instance with optional HKDF info |
448
+ | `generateKeyPair()` | ECDH | `Promise<{ keyPair: CryptoKeyPair, publicKeyB64: string }>` | Generate P-256 key pair |
449
+ | `importPublicKey(opts)` | ECDH | `Promise<CryptoKey>` | Import peer's base64 public key |
450
+ | `deriveAESKey(opts)` | ECDH | `Promise<{ key: CryptoKey, salt: string }>` | Derive AES-256-GCM key via HKDF |
451
+ | `encrypt(opts)` | ECDH | `Promise<IECDHEncryptedPayload>` | Encrypt with derived AES key |
452
+ | `decrypt(opts)` | ECDH | `Promise<string>` | Decrypt with derived AES key |
453
+ | `hash(text, options)` | _(function)_ | `string` | MD5 or SHA256 HMAC hash |
454
+
455
+ ## Troubleshooting
456
+
457
+ ### "[validateAlgorithmName] Invalid algorithm name | algorithm: undefined"
458
+
459
+ **Cause:** An empty or undefined `algorithm` string was passed to the constructor (or `withAlgorithm()`).
460
+
461
+ **Fix:** Provide a valid algorithm name:
462
+
463
+ ```typescript
464
+ const aes = AES.withAlgorithm('aes-256-gcm'); // Not undefined or empty
465
+ const rsa = RSA.withAlgorithm(); // No parameter needed
466
+ const ecdh = ECDH.withAlgorithm(); // No parameter needed
467
+ ```
468
+
469
+ ### "[ECDH.fromBase64] Invalid base64 input"
470
+
471
+ **Cause:** A value passed to an ECDH method (public key, salt, or encrypted payload) is not valid base64. The string must have a length divisible by 4 and contain only characters `A-Za-z0-9+/=`.
472
+
473
+ **Fix:** Ensure all base64 strings are passed as-is from the methods that produced them (`publicKeyB64`, `salt`, `iv`, `ct`). Do not trim, re-encode, or modify these values.
474
+
475
+ ### "Unsupported state or unable to authenticate data"
476
+
477
+ **Cause:** The ciphertext or auth tag was modified in transit, or you are decrypting GCM ciphertext with a CBC instance (or vice versa). CBC and GCM produce incompatible ciphertext formats.
478
+
479
+ **Fix:** Ensure the same algorithm mode is used for both encrypt and decrypt:
480
+
481
+ ```typescript
482
+ // Both must use the same mode
483
+ const aes = AES.withAlgorithm('aes-256-gcm');
484
+ const encrypted = aes.encrypt({ message, secret });
485
+ const decrypted = aes.decrypt({ message: encrypted, secret }); // same instance or same mode
486
+ ```
487
+
488
+ ### ECDH decrypt throws even though both sides used each other's public keys
489
+
490
+ **Cause:** Each call to `deriveAESKey` without a `salt` parameter generates a new random 32-byte salt. If both sides generate their own salt, they derive different AES keys.
491
+
492
+ **Fix:** The initiator calls `deriveAESKey` without `salt` (generates one), then sends the returned `salt` string to the responder. The responder passes that `salt` into their `deriveAESKey` call.
493
+
494
+ ```typescript
495
+ // Initiator
496
+ const { key: aliceKey, salt } = await ecdh.deriveAESKey({
497
+ privateKey: alice.keyPair.privateKey,
498
+ peerPublicKey: bobPub,
499
+ });
500
+ // Send `salt` to responder
501
+
502
+ // Responder
503
+ const { key: bobKey } = await ecdh.deriveAESKey({
504
+ privateKey: bob.keyPair.privateKey,
505
+ peerPublicKey: alicePub,
506
+ salt, // <-- use initiator's salt
507
+ });
508
+ ```
509
+
510
+ ### SHA256 hash returns the original text instead of a hash
511
+
512
+ **Cause:** The `SHA256` algorithm uses `createHmac` internally, which requires a `secret` parameter. When `secret` is `undefined`, the function short-circuits and returns the original text.
513
+
514
+ **Fix:** Always provide a `secret` when using `SHA256`:
515
+
516
+ ```typescript
517
+ const hashed = hash('text', {
518
+ algorithm: 'SHA256',
519
+ secret: 'my-hmac-key',
520
+ outputType: 'hex',
521
+ });
522
+ ```
523
+
524
+ ## See Also
525
+
526
+ - **Related Concepts:**
527
+ - [Services](/guides/core-concepts/services) -- Password hashing in user services
528
+
529
+ - **Other Helpers:**
530
+ - [Helpers Index](../index) -- All available helpers
531
+
532
+ - **References:**
533
+ - [Crypto Utility](/references/utilities/crypto) -- Pure crypto utilities
534
+ - [Authentication Component](/references/components/authentication/) -- JWT and password verification
535
+
536
+ - **Best Practices:**
537
+ - [Security Guidelines](/best-practices/security-guidelines) -- Cryptographic best practices