@molecule/api-encryption 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +261 -0
- package/package.json +7 -6
package/README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:48:05.661Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-encryption
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Encryption core interface for molecule.dev.
|
|
16
|
+
|
|
17
|
+
Provides the `EncryptionProvider` interface for field-level encryption,
|
|
18
|
+
decryption, hashing, and key rotation. Bond a concrete provider
|
|
19
|
+
(e.g. `@molecule/api-encryption-aes`) at startup via `setProvider()`.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { setProvider, encrypt, decrypt, hash, verify } from '@molecule/api-encryption'
|
|
25
|
+
import { provider } from '@molecule/api-encryption-aes'
|
|
26
|
+
|
|
27
|
+
// Wire the provider at startup
|
|
28
|
+
setProvider(provider)
|
|
29
|
+
|
|
30
|
+
// Field-level encryption for sensitive data at rest
|
|
31
|
+
const ciphertext = await encrypt(accessToken)
|
|
32
|
+
const plaintext = await decrypt(ciphertext)
|
|
33
|
+
|
|
34
|
+
// Integrity hashing (checksums, dedupe keys) — NOT for passwords
|
|
35
|
+
const checksum = await hash(documentBody)
|
|
36
|
+
const untampered = await verify(documentBody, checksum)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Type
|
|
40
|
+
|
|
41
|
+
`core`
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @molecule/api-encryption @molecule/api-bond @molecule/api-i18n
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### Interfaces
|
|
52
|
+
|
|
53
|
+
#### `EncryptionConfig`
|
|
54
|
+
|
|
55
|
+
Configuration options for an encryption provider.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
interface EncryptionConfig {
|
|
59
|
+
/** The encryption key (or key identifier for KMS-backed providers). */
|
|
60
|
+
key: string
|
|
61
|
+
|
|
62
|
+
/** Optional algorithm identifier (provider-specific). */
|
|
63
|
+
algorithm?: string
|
|
64
|
+
|
|
65
|
+
/** Optional key version for rotation tracking. */
|
|
66
|
+
keyVersion?: number
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### `EncryptionProvider`
|
|
71
|
+
|
|
72
|
+
Encryption provider interface.
|
|
73
|
+
|
|
74
|
+
All encryption providers must implement this interface to provide
|
|
75
|
+
field-level encryption, decryption, hashing, and key rotation.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
interface EncryptionProvider {
|
|
79
|
+
/**
|
|
80
|
+
* Encrypts a plaintext string.
|
|
81
|
+
*
|
|
82
|
+
* @param plaintext - The data to encrypt.
|
|
83
|
+
* @param context - Optional additional authenticated data (AAD) for
|
|
84
|
+
* authenticated encryption schemes.
|
|
85
|
+
* @returns The encrypted ciphertext string (typically base64-encoded).
|
|
86
|
+
*/
|
|
87
|
+
encrypt(plaintext: string, context?: string): Promise<string>
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Decrypts a ciphertext string.
|
|
91
|
+
*
|
|
92
|
+
* @param ciphertext - The encrypted data to decrypt.
|
|
93
|
+
* @param context - Optional additional authenticated data (AAD) that was
|
|
94
|
+
* used during encryption.
|
|
95
|
+
* @returns The decrypted plaintext string.
|
|
96
|
+
*/
|
|
97
|
+
decrypt(ciphertext: string, context?: string): Promise<string>
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Produces a one-way hash of the given data.
|
|
101
|
+
*
|
|
102
|
+
* @param data - The data to hash.
|
|
103
|
+
* @returns The hash string (hex or base64 encoded).
|
|
104
|
+
*/
|
|
105
|
+
hash(data: string): Promise<string>
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Verifies that data matches a previously computed hash.
|
|
109
|
+
*
|
|
110
|
+
* @param data - The original data to verify.
|
|
111
|
+
* @param hash - The hash to verify against.
|
|
112
|
+
* @returns `true` if the data matches the hash.
|
|
113
|
+
*/
|
|
114
|
+
verify(data: string, hash: string): Promise<boolean>
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Rotates the encryption key. Re-encrypts internal state or markers
|
|
118
|
+
* so that future operations use the new key while previously encrypted
|
|
119
|
+
* data can still be decrypted during a transition period.
|
|
120
|
+
*
|
|
121
|
+
* @param oldKey - The current encryption key.
|
|
122
|
+
* @param newKey - The new encryption key to rotate to.
|
|
123
|
+
*/
|
|
124
|
+
rotateKey(oldKey: string, newKey: string): Promise<void>
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Functions
|
|
129
|
+
|
|
130
|
+
#### `decrypt(ciphertext, context)`
|
|
131
|
+
|
|
132
|
+
Decrypts a ciphertext string using the bonded provider.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
function decrypt(ciphertext: string, context?: string): Promise<string>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- `ciphertext` — The encrypted data to decrypt.
|
|
139
|
+
- `context` — Optional additional authenticated data (AAD).
|
|
140
|
+
|
|
141
|
+
**Returns:** The decrypted plaintext string.
|
|
142
|
+
|
|
143
|
+
#### `encrypt(plaintext, context)`
|
|
144
|
+
|
|
145
|
+
Encrypts a plaintext string using the bonded provider.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
function encrypt(plaintext: string, context?: string): Promise<string>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- `plaintext` — The data to encrypt.
|
|
152
|
+
- `context` — Optional additional authenticated data (AAD).
|
|
153
|
+
|
|
154
|
+
**Returns:** The encrypted ciphertext string.
|
|
155
|
+
|
|
156
|
+
#### `getProvider()`
|
|
157
|
+
|
|
158
|
+
Retrieves the bonded encryption provider, throwing if none is configured.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
function getProvider(): EncryptionProvider
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Returns:** The bonded encryption provider.
|
|
165
|
+
|
|
166
|
+
#### `hash(data)`
|
|
167
|
+
|
|
168
|
+
Produces a one-way hash of the given data using the bonded provider.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
function hash(data: string): Promise<string>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
- `data` — The data to hash.
|
|
175
|
+
|
|
176
|
+
**Returns:** The hash string.
|
|
177
|
+
|
|
178
|
+
#### `hasProvider()`
|
|
179
|
+
|
|
180
|
+
Checks whether an encryption provider is currently bonded.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
function hasProvider(): boolean
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**Returns:** `true` if an encryption provider is bonded.
|
|
187
|
+
|
|
188
|
+
#### `rotateKey(oldKey, newKey)`
|
|
189
|
+
|
|
190
|
+
Rotates the encryption key using the bonded provider.
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
function rotateKey(oldKey: string, newKey: string): Promise<void>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
- `oldKey` — The current encryption key.
|
|
197
|
+
- `newKey` — The new encryption key to rotate to.
|
|
198
|
+
|
|
199
|
+
**Returns:** Resolves when rotation completes successfully.
|
|
200
|
+
|
|
201
|
+
#### `setProvider(provider)`
|
|
202
|
+
|
|
203
|
+
Registers an encryption provider as the active singleton. Called by
|
|
204
|
+
bond packages during application startup.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
function setProvider(provider: EncryptionProvider): void
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
- `provider` — The encryption provider implementation to bond.
|
|
211
|
+
|
|
212
|
+
#### `verify(data, hashed)`
|
|
213
|
+
|
|
214
|
+
Verifies that data matches a previously computed hash.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
function verify(data: string, hashed: string): Promise<boolean>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
- `data` — The original data to verify.
|
|
221
|
+
- `hashed` — The hash to verify against.
|
|
222
|
+
|
|
223
|
+
**Returns:** `true` if the data matches the hash.
|
|
224
|
+
|
|
225
|
+
## Available Providers
|
|
226
|
+
|
|
227
|
+
| Provider | Package |
|
|
228
|
+
| ---------- | ------------------------------ |
|
|
229
|
+
| Encryption | `@molecule/api-encryption-aes` |
|
|
230
|
+
|
|
231
|
+
## Injection Notes
|
|
232
|
+
|
|
233
|
+
### Requirements
|
|
234
|
+
|
|
235
|
+
Peer dependencies:
|
|
236
|
+
|
|
237
|
+
- `@molecule/api-bond` ^1.0.1
|
|
238
|
+
- `@molecule/api-i18n` ^1.0.1
|
|
239
|
+
|
|
240
|
+
### Runtime Dependencies
|
|
241
|
+
|
|
242
|
+
- `@molecule/api-bond`
|
|
243
|
+
- `@molecule/api-i18n`
|
|
244
|
+
|
|
245
|
+
- **NEVER hash passwords with `hash()`.** It is a fast, unsalted integrity
|
|
246
|
+
hash (e.g. SHA-256 in the AES bond) — fine for checksums and dedupe keys,
|
|
247
|
+
catastrophic for credentials. Passwords go through
|
|
248
|
+
`@molecule/api-password` (salted, slow KDF).
|
|
249
|
+
- **The key is a server-side secret** (e.g. the AES bond's `ENCRYPTION_KEY`,
|
|
250
|
+
auto-generated at scaffold). Never hardcode, log, or expose it — and a
|
|
251
|
+
lost key makes every ciphertext permanently unreadable, so treat key
|
|
252
|
+
changes as a rotation (`rotateKey`), never an edit.
|
|
253
|
+
- **Ciphertext is opaque.** An encrypted DB column cannot be filtered,
|
|
254
|
+
sorted, or `like`-searched on its plaintext. Encrypt narrow sensitive
|
|
255
|
+
fields (tokens, PII), not fields you query by.
|
|
256
|
+
- **`context` (AAD) must match at decrypt.** If you pass a context to
|
|
257
|
+
`encrypt(value, context)`, the identical context is required to decrypt.
|
|
258
|
+
Binding a ciphertext to e.g. its record id stops cross-row copy-paste —
|
|
259
|
+
but then the id can never change.
|
|
260
|
+
- `decrypt()` throws on a wrong key or tampered ciphertext — treat that as
|
|
261
|
+
corruption/misconfiguration to surface, not a condition to retry.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-encryption",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Field-level encryption core interface for molecule.dev — encrypt, decrypt, hash, verify, and rotate keys",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
21
22
|
],
|
|
22
23
|
"keywords": [
|
|
23
24
|
"molecule",
|
|
@@ -28,12 +29,12 @@
|
|
|
28
29
|
],
|
|
29
30
|
"license": "Apache-2.0",
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@molecule/api-bond": "^1.0.
|
|
32
|
-
"@molecule/api-i18n": "^1.0.
|
|
32
|
+
"@molecule/api-bond": "^1.0.1",
|
|
33
|
+
"@molecule/api-i18n": "^1.0.1"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@molecule/api-bond": "1.0.
|
|
36
|
-
"@molecule/api-i18n": "1.0.
|
|
36
|
+
"@molecule/api-bond": "1.0.1",
|
|
37
|
+
"@molecule/api-i18n": "1.0.1",
|
|
37
38
|
"@types/node": "26.1.2",
|
|
38
39
|
"typescript": "6.0.3",
|
|
39
40
|
"vitest": "4.1.10"
|