@molecule/api-encryption 1.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.
- package/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/provider.d.ts +75 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +100 -0
- package/dist/types.d.ts +71 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-encryption` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-encryption is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encryption core interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides the `EncryptionProvider` interface for field-level encryption,
|
|
5
|
+
* decryption, hashing, and key rotation. Bond a concrete provider
|
|
6
|
+
* (e.g. `@molecule/api-encryption-aes`) at startup via `setProvider()`.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { setProvider, encrypt, decrypt, hash, verify } from '@molecule/api-encryption'
|
|
12
|
+
* import { provider } from '@molecule/api-encryption-aes'
|
|
13
|
+
*
|
|
14
|
+
* // Wire the provider at startup
|
|
15
|
+
* setProvider(provider)
|
|
16
|
+
*
|
|
17
|
+
* // Field-level encryption for sensitive data at rest
|
|
18
|
+
* const ciphertext = await encrypt(accessToken)
|
|
19
|
+
* const plaintext = await decrypt(ciphertext)
|
|
20
|
+
*
|
|
21
|
+
* // Integrity hashing (checksums, dedupe keys) — NOT for passwords
|
|
22
|
+
* const checksum = await hash(documentBody)
|
|
23
|
+
* const untampered = await verify(documentBody, checksum)
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* - **NEVER hash passwords with `hash()`.** It is a fast, unsalted integrity
|
|
28
|
+
* hash (e.g. SHA-256 in the AES bond) — fine for checksums and dedupe keys,
|
|
29
|
+
* catastrophic for credentials. Passwords go through
|
|
30
|
+
* `@molecule/api-password` (salted, slow KDF).
|
|
31
|
+
* - **The key is a server-side secret** (e.g. the AES bond's `ENCRYPTION_KEY`,
|
|
32
|
+
* auto-generated at scaffold). Never hardcode, log, or expose it — and a
|
|
33
|
+
* lost key makes every ciphertext permanently unreadable, so treat key
|
|
34
|
+
* changes as a rotation (`rotateKey`), never an edit.
|
|
35
|
+
* - **Ciphertext is opaque.** An encrypted DB column cannot be filtered,
|
|
36
|
+
* sorted, or `like`-searched on its plaintext. Encrypt narrow sensitive
|
|
37
|
+
* fields (tokens, PII), not fields you query by.
|
|
38
|
+
* - **`context` (AAD) must match at decrypt.** If you pass a context to
|
|
39
|
+
* `encrypt(value, context)`, the identical context is required to decrypt.
|
|
40
|
+
* Binding a ciphertext to e.g. its record id stops cross-row copy-paste —
|
|
41
|
+
* but then the id can never change.
|
|
42
|
+
* - `decrypt()` throws on a wrong key or tampered ciphertext — treat that as
|
|
43
|
+
* corruption/misconfiguration to surface, not a condition to retry.
|
|
44
|
+
*/
|
|
45
|
+
export * from './browser-guard.js';
|
|
46
|
+
export * from './provider.js';
|
|
47
|
+
export * from './types.js';
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encryption core interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Provides the `EncryptionProvider` interface for field-level encryption,
|
|
5
|
+
* decryption, hashing, and key rotation. Bond a concrete provider
|
|
6
|
+
* (e.g. `@molecule/api-encryption-aes`) at startup via `setProvider()`.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { setProvider, encrypt, decrypt, hash, verify } from '@molecule/api-encryption'
|
|
12
|
+
* import { provider } from '@molecule/api-encryption-aes'
|
|
13
|
+
*
|
|
14
|
+
* // Wire the provider at startup
|
|
15
|
+
* setProvider(provider)
|
|
16
|
+
*
|
|
17
|
+
* // Field-level encryption for sensitive data at rest
|
|
18
|
+
* const ciphertext = await encrypt(accessToken)
|
|
19
|
+
* const plaintext = await decrypt(ciphertext)
|
|
20
|
+
*
|
|
21
|
+
* // Integrity hashing (checksums, dedupe keys) — NOT for passwords
|
|
22
|
+
* const checksum = await hash(documentBody)
|
|
23
|
+
* const untampered = await verify(documentBody, checksum)
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* - **NEVER hash passwords with `hash()`.** It is a fast, unsalted integrity
|
|
28
|
+
* hash (e.g. SHA-256 in the AES bond) — fine for checksums and dedupe keys,
|
|
29
|
+
* catastrophic for credentials. Passwords go through
|
|
30
|
+
* `@molecule/api-password` (salted, slow KDF).
|
|
31
|
+
* - **The key is a server-side secret** (e.g. the AES bond's `ENCRYPTION_KEY`,
|
|
32
|
+
* auto-generated at scaffold). Never hardcode, log, or expose it — and a
|
|
33
|
+
* lost key makes every ciphertext permanently unreadable, so treat key
|
|
34
|
+
* changes as a rotation (`rotateKey`), never an edit.
|
|
35
|
+
* - **Ciphertext is opaque.** An encrypted DB column cannot be filtered,
|
|
36
|
+
* sorted, or `like`-searched on its plaintext. Encrypt narrow sensitive
|
|
37
|
+
* fields (tokens, PII), not fields you query by.
|
|
38
|
+
* - **`context` (AAD) must match at decrypt.** If you pass a context to
|
|
39
|
+
* `encrypt(value, context)`, the identical context is required to decrypt.
|
|
40
|
+
* Binding a ciphertext to e.g. its record id stops cross-row copy-paste —
|
|
41
|
+
* but then the id can never change.
|
|
42
|
+
* - `decrypt()` throws on a wrong key or tampered ciphertext — treat that as
|
|
43
|
+
* corruption/misconfiguration to surface, not a condition to retry.
|
|
44
|
+
*/
|
|
45
|
+
export * from './browser-guard.js';
|
|
46
|
+
export * from './provider.js';
|
|
47
|
+
export * from './types.js';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encryption provider bond accessor and convenience functions.
|
|
3
|
+
*
|
|
4
|
+
* Bond packages (e.g. `@molecule/api-encryption-aes`) call `setProvider()`
|
|
5
|
+
* during setup. Application code uses the convenience functions which
|
|
6
|
+
* delegate to the bonded provider.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { EncryptionProvider } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Registers an encryption provider as the active singleton. Called by
|
|
13
|
+
* bond packages during application startup.
|
|
14
|
+
*
|
|
15
|
+
* @param provider - The encryption provider implementation to bond.
|
|
16
|
+
*/
|
|
17
|
+
export declare const setProvider: (provider: EncryptionProvider) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves the bonded encryption provider, throwing if none is configured.
|
|
20
|
+
*
|
|
21
|
+
* @returns The bonded encryption provider.
|
|
22
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getProvider: () => EncryptionProvider;
|
|
25
|
+
/**
|
|
26
|
+
* Checks whether an encryption provider is currently bonded.
|
|
27
|
+
*
|
|
28
|
+
* @returns `true` if an encryption provider is bonded.
|
|
29
|
+
*/
|
|
30
|
+
export declare const hasProvider: () => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Encrypts a plaintext string using the bonded provider.
|
|
33
|
+
*
|
|
34
|
+
* @param plaintext - The data to encrypt.
|
|
35
|
+
* @param context - Optional additional authenticated data (AAD).
|
|
36
|
+
* @returns The encrypted ciphertext string.
|
|
37
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
38
|
+
*/
|
|
39
|
+
export declare const encrypt: (plaintext: string, context?: string) => Promise<string>;
|
|
40
|
+
/**
|
|
41
|
+
* Decrypts a ciphertext string using the bonded provider.
|
|
42
|
+
*
|
|
43
|
+
* @param ciphertext - The encrypted data to decrypt.
|
|
44
|
+
* @param context - Optional additional authenticated data (AAD).
|
|
45
|
+
* @returns The decrypted plaintext string.
|
|
46
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
47
|
+
*/
|
|
48
|
+
export declare const decrypt: (ciphertext: string, context?: string) => Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Produces a one-way hash of the given data using the bonded provider.
|
|
51
|
+
*
|
|
52
|
+
* @param data - The data to hash.
|
|
53
|
+
* @returns The hash string.
|
|
54
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
55
|
+
*/
|
|
56
|
+
export declare const hash: (data: string) => Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Verifies that data matches a previously computed hash.
|
|
59
|
+
*
|
|
60
|
+
* @param data - The original data to verify.
|
|
61
|
+
* @param hashed - The hash to verify against.
|
|
62
|
+
* @returns `true` if the data matches the hash.
|
|
63
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
64
|
+
*/
|
|
65
|
+
export declare const verify: (data: string, hashed: string) => Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Rotates the encryption key using the bonded provider.
|
|
68
|
+
*
|
|
69
|
+
* @param oldKey - The current encryption key.
|
|
70
|
+
* @param newKey - The new encryption key to rotate to.
|
|
71
|
+
* @returns Resolves when rotation completes successfully.
|
|
72
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
73
|
+
*/
|
|
74
|
+
export declare const rotateKey: (oldKey: string, newKey: string) => Promise<void>;
|
|
75
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAKpD;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,kBAAkB,KAAG,IAE1D,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,QAAO,kBAW9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,QAAO,OAE9B,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAU,WAAW,MAAM,EAAE,UAAU,MAAM,KAAG,OAAO,CAAC,MAAM,CAEjF,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAU,YAAY,MAAM,EAAE,UAAU,MAAM,KAAG,OAAO,CAAC,MAAM,CAElF,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,GAAU,MAAM,MAAM,KAAG,OAAO,CAAC,MAAM,CAEvD,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,MAAM,GAAU,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,OAAO,CAE1E,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,GAAU,QAAQ,MAAM,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,IAAI,CAE5E,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encryption provider bond accessor and convenience functions.
|
|
3
|
+
*
|
|
4
|
+
* Bond packages (e.g. `@molecule/api-encryption-aes`) call `setProvider()`
|
|
5
|
+
* during setup. Application code uses the convenience functions which
|
|
6
|
+
* delegate to the bonded provider.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { bond, expectBond, isBonded, require as bondRequire } from '@molecule/api-bond';
|
|
11
|
+
import { t } from '@molecule/api-i18n';
|
|
12
|
+
const BOND_TYPE = 'encryption';
|
|
13
|
+
expectBond(BOND_TYPE);
|
|
14
|
+
/**
|
|
15
|
+
* Registers an encryption provider as the active singleton. Called by
|
|
16
|
+
* bond packages during application startup.
|
|
17
|
+
*
|
|
18
|
+
* @param provider - The encryption provider implementation to bond.
|
|
19
|
+
*/
|
|
20
|
+
export const setProvider = (provider) => {
|
|
21
|
+
bond(BOND_TYPE, provider);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the bonded encryption provider, throwing if none is configured.
|
|
25
|
+
*
|
|
26
|
+
* @returns The bonded encryption provider.
|
|
27
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
28
|
+
*/
|
|
29
|
+
export const getProvider = () => {
|
|
30
|
+
try {
|
|
31
|
+
return bondRequire(BOND_TYPE);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new Error(t('encryption.error.noProvider', undefined, {
|
|
35
|
+
defaultValue: 'Encryption provider not configured. Call setProvider() first.',
|
|
36
|
+
}), { cause: error });
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether an encryption provider is currently bonded.
|
|
41
|
+
*
|
|
42
|
+
* @returns `true` if an encryption provider is bonded.
|
|
43
|
+
*/
|
|
44
|
+
export const hasProvider = () => {
|
|
45
|
+
return isBonded(BOND_TYPE);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Encrypts a plaintext string using the bonded provider.
|
|
49
|
+
*
|
|
50
|
+
* @param plaintext - The data to encrypt.
|
|
51
|
+
* @param context - Optional additional authenticated data (AAD).
|
|
52
|
+
* @returns The encrypted ciphertext string.
|
|
53
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
54
|
+
*/
|
|
55
|
+
export const encrypt = async (plaintext, context) => {
|
|
56
|
+
return getProvider().encrypt(plaintext, context);
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Decrypts a ciphertext string using the bonded provider.
|
|
60
|
+
*
|
|
61
|
+
* @param ciphertext - The encrypted data to decrypt.
|
|
62
|
+
* @param context - Optional additional authenticated data (AAD).
|
|
63
|
+
* @returns The decrypted plaintext string.
|
|
64
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
65
|
+
*/
|
|
66
|
+
export const decrypt = async (ciphertext, context) => {
|
|
67
|
+
return getProvider().decrypt(ciphertext, context);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Produces a one-way hash of the given data using the bonded provider.
|
|
71
|
+
*
|
|
72
|
+
* @param data - The data to hash.
|
|
73
|
+
* @returns The hash string.
|
|
74
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
75
|
+
*/
|
|
76
|
+
export const hash = async (data) => {
|
|
77
|
+
return getProvider().hash(data);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Verifies that data matches a previously computed hash.
|
|
81
|
+
*
|
|
82
|
+
* @param data - The original data to verify.
|
|
83
|
+
* @param hashed - The hash to verify against.
|
|
84
|
+
* @returns `true` if the data matches the hash.
|
|
85
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
86
|
+
*/
|
|
87
|
+
export const verify = async (data, hashed) => {
|
|
88
|
+
return getProvider().verify(data, hashed);
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Rotates the encryption key using the bonded provider.
|
|
92
|
+
*
|
|
93
|
+
* @param oldKey - The current encryption key.
|
|
94
|
+
* @param newKey - The new encryption key to rotate to.
|
|
95
|
+
* @returns Resolves when rotation completes successfully.
|
|
96
|
+
* @throws {Error} If no encryption provider has been bonded.
|
|
97
|
+
*/
|
|
98
|
+
export const rotateKey = async (oldKey, newKey) => {
|
|
99
|
+
return getProvider().rotateKey(oldKey, newKey);
|
|
100
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the encryption core interface.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `EncryptionProvider` interface for field-level encryption,
|
|
5
|
+
* decryption, hashing, and key rotation. Bond packages implement this
|
|
6
|
+
* interface to provide concrete encryption algorithms (AES-256-GCM, etc.).
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Configuration options for an encryption provider.
|
|
12
|
+
*/
|
|
13
|
+
export interface EncryptionConfig {
|
|
14
|
+
/** The encryption key (or key identifier for KMS-backed providers). */
|
|
15
|
+
key: string;
|
|
16
|
+
/** Optional algorithm identifier (provider-specific). */
|
|
17
|
+
algorithm?: string;
|
|
18
|
+
/** Optional key version for rotation tracking. */
|
|
19
|
+
keyVersion?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Encryption provider interface.
|
|
23
|
+
*
|
|
24
|
+
* All encryption providers must implement this interface to provide
|
|
25
|
+
* field-level encryption, decryption, hashing, and key rotation.
|
|
26
|
+
*/
|
|
27
|
+
export interface EncryptionProvider {
|
|
28
|
+
/**
|
|
29
|
+
* Encrypts a plaintext string.
|
|
30
|
+
*
|
|
31
|
+
* @param plaintext - The data to encrypt.
|
|
32
|
+
* @param context - Optional additional authenticated data (AAD) for
|
|
33
|
+
* authenticated encryption schemes.
|
|
34
|
+
* @returns The encrypted ciphertext string (typically base64-encoded).
|
|
35
|
+
*/
|
|
36
|
+
encrypt(plaintext: string, context?: string): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Decrypts a ciphertext string.
|
|
39
|
+
*
|
|
40
|
+
* @param ciphertext - The encrypted data to decrypt.
|
|
41
|
+
* @param context - Optional additional authenticated data (AAD) that was
|
|
42
|
+
* used during encryption.
|
|
43
|
+
* @returns The decrypted plaintext string.
|
|
44
|
+
*/
|
|
45
|
+
decrypt(ciphertext: string, context?: string): Promise<string>;
|
|
46
|
+
/**
|
|
47
|
+
* Produces a one-way hash of the given data.
|
|
48
|
+
*
|
|
49
|
+
* @param data - The data to hash.
|
|
50
|
+
* @returns The hash string (hex or base64 encoded).
|
|
51
|
+
*/
|
|
52
|
+
hash(data: string): Promise<string>;
|
|
53
|
+
/**
|
|
54
|
+
* Verifies that data matches a previously computed hash.
|
|
55
|
+
*
|
|
56
|
+
* @param data - The original data to verify.
|
|
57
|
+
* @param hash - The hash to verify against.
|
|
58
|
+
* @returns `true` if the data matches the hash.
|
|
59
|
+
*/
|
|
60
|
+
verify(data: string, hash: string): Promise<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* Rotates the encryption key. Re-encrypts internal state or markers
|
|
63
|
+
* so that future operations use the new key while previously encrypted
|
|
64
|
+
* data can still be decrypted during a transition period.
|
|
65
|
+
*
|
|
66
|
+
* @param oldKey - The current encryption key.
|
|
67
|
+
* @param newKey - The new encryption key to rotate to.
|
|
68
|
+
*/
|
|
69
|
+
rotateKey(oldKey: string, newKey: string): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,uEAAuE;IACvE,GAAG,EAAE,MAAM,CAAA;IAEX,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,kDAAkD;IAClD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE7D;;;;;;;OAOG;IACH,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE9D;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAEnC;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAEpD;;;;;;;OAOG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACzD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the encryption core interface.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `EncryptionProvider` interface for field-level encryption,
|
|
5
|
+
* decryption, hashing, and key rotation. Bond packages implement this
|
|
6
|
+
* interface to provide concrete encryption algorithms (AES-256-GCM, etc.).
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-encryption",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Field-level encryption core interface for molecule.dev — encrypt, decrypt, hash, verify, and rotate keys",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"encryption",
|
|
25
|
+
"aes",
|
|
26
|
+
"field-level-encryption",
|
|
27
|
+
"key-rotation"
|
|
28
|
+
],
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@molecule/api-bond": "^1.0.0",
|
|
32
|
+
"@molecule/api-i18n": "^1.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@molecule/api-bond": "1.0.0",
|
|
36
|
+
"@molecule/api-i18n": "1.0.0",
|
|
37
|
+
"@types/node": "26.1.2",
|
|
38
|
+
"typescript": "6.0.3",
|
|
39
|
+
"vitest": "4.1.10"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
44
|
+
"directory": "packages/api/core/encryption"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/core/encryption",
|
|
47
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|