rehydra 0.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/LICENSE +22 -0
- package/README.md +615 -0
- package/dist/crypto/index.d.ts +6 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/index.js +6 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/crypto/pii-map-crypto.d.ts +114 -0
- package/dist/crypto/pii-map-crypto.d.ts.map +1 -0
- package/dist/crypto/pii-map-crypto.js +228 -0
- package/dist/crypto/pii-map-crypto.js.map +1 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +384 -0
- package/dist/index.js.map +1 -0
- package/dist/ner/bio-decoder.d.ts +64 -0
- package/dist/ner/bio-decoder.d.ts.map +1 -0
- package/dist/ner/bio-decoder.js +216 -0
- package/dist/ner/bio-decoder.js.map +1 -0
- package/dist/ner/index.d.ts +10 -0
- package/dist/ner/index.d.ts.map +1 -0
- package/dist/ner/index.js +10 -0
- package/dist/ner/index.js.map +1 -0
- package/dist/ner/model-manager.d.ts +111 -0
- package/dist/ner/model-manager.d.ts.map +1 -0
- package/dist/ner/model-manager.js +325 -0
- package/dist/ner/model-manager.js.map +1 -0
- package/dist/ner/ner-model.d.ts +114 -0
- package/dist/ner/ner-model.d.ts.map +1 -0
- package/dist/ner/ner-model.js +253 -0
- package/dist/ner/ner-model.js.map +1 -0
- package/dist/ner/onnx-runtime.d.ts +46 -0
- package/dist/ner/onnx-runtime.d.ts.map +1 -0
- package/dist/ner/onnx-runtime.js +130 -0
- package/dist/ner/onnx-runtime.js.map +1 -0
- package/dist/ner/tokenizer.d.ts +118 -0
- package/dist/ner/tokenizer.d.ts.map +1 -0
- package/dist/ner/tokenizer.js +332 -0
- package/dist/ner/tokenizer.js.map +1 -0
- package/dist/pipeline/index.d.ts +12 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +12 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/prenormalize.d.ts +48 -0
- package/dist/pipeline/prenormalize.d.ts.map +1 -0
- package/dist/pipeline/prenormalize.js +94 -0
- package/dist/pipeline/prenormalize.js.map +1 -0
- package/dist/pipeline/resolver.d.ts +56 -0
- package/dist/pipeline/resolver.d.ts.map +1 -0
- package/dist/pipeline/resolver.js +239 -0
- package/dist/pipeline/resolver.js.map +1 -0
- package/dist/pipeline/semantic-data-loader.d.ts +165 -0
- package/dist/pipeline/semantic-data-loader.d.ts.map +1 -0
- package/dist/pipeline/semantic-data-loader.js +655 -0
- package/dist/pipeline/semantic-data-loader.js.map +1 -0
- package/dist/pipeline/semantic-enricher.d.ts +112 -0
- package/dist/pipeline/semantic-enricher.d.ts.map +1 -0
- package/dist/pipeline/semantic-enricher.js +318 -0
- package/dist/pipeline/semantic-enricher.js.map +1 -0
- package/dist/pipeline/tagger.d.ts +114 -0
- package/dist/pipeline/tagger.d.ts.map +1 -0
- package/dist/pipeline/tagger.js +374 -0
- package/dist/pipeline/tagger.js.map +1 -0
- package/dist/pipeline/title-extractor.d.ts +79 -0
- package/dist/pipeline/title-extractor.d.ts.map +1 -0
- package/dist/pipeline/title-extractor.js +801 -0
- package/dist/pipeline/title-extractor.js.map +1 -0
- package/dist/pipeline/validator.d.ts +65 -0
- package/dist/pipeline/validator.d.ts.map +1 -0
- package/dist/pipeline/validator.js +264 -0
- package/dist/pipeline/validator.js.map +1 -0
- package/dist/recognizers/base.d.ts +78 -0
- package/dist/recognizers/base.d.ts.map +1 -0
- package/dist/recognizers/base.js +100 -0
- package/dist/recognizers/base.js.map +1 -0
- package/dist/recognizers/bic-swift.d.ts +10 -0
- package/dist/recognizers/bic-swift.d.ts.map +1 -0
- package/dist/recognizers/bic-swift.js +107 -0
- package/dist/recognizers/bic-swift.js.map +1 -0
- package/dist/recognizers/credit-card.d.ts +32 -0
- package/dist/recognizers/credit-card.d.ts.map +1 -0
- package/dist/recognizers/credit-card.js +160 -0
- package/dist/recognizers/credit-card.js.map +1 -0
- package/dist/recognizers/custom-id.d.ts +28 -0
- package/dist/recognizers/custom-id.d.ts.map +1 -0
- package/dist/recognizers/custom-id.js +116 -0
- package/dist/recognizers/custom-id.js.map +1 -0
- package/dist/recognizers/email.d.ts +10 -0
- package/dist/recognizers/email.d.ts.map +1 -0
- package/dist/recognizers/email.js +75 -0
- package/dist/recognizers/email.js.map +1 -0
- package/dist/recognizers/iban.d.ts +14 -0
- package/dist/recognizers/iban.d.ts.map +1 -0
- package/dist/recognizers/iban.js +67 -0
- package/dist/recognizers/iban.js.map +1 -0
- package/dist/recognizers/index.d.ts +20 -0
- package/dist/recognizers/index.d.ts.map +1 -0
- package/dist/recognizers/index.js +42 -0
- package/dist/recognizers/index.js.map +1 -0
- package/dist/recognizers/ip-address.d.ts +14 -0
- package/dist/recognizers/ip-address.d.ts.map +1 -0
- package/dist/recognizers/ip-address.js +183 -0
- package/dist/recognizers/ip-address.js.map +1 -0
- package/dist/recognizers/phone.d.ts +10 -0
- package/dist/recognizers/phone.d.ts.map +1 -0
- package/dist/recognizers/phone.js +145 -0
- package/dist/recognizers/phone.js.map +1 -0
- package/dist/recognizers/registry.d.ts +59 -0
- package/dist/recognizers/registry.d.ts.map +1 -0
- package/dist/recognizers/registry.js +113 -0
- package/dist/recognizers/registry.js.map +1 -0
- package/dist/recognizers/url.d.ts +14 -0
- package/dist/recognizers/url.d.ts.map +1 -0
- package/dist/recognizers/url.js +121 -0
- package/dist/recognizers/url.js.map +1 -0
- package/dist/types/index.d.ts +197 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +80 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/pii-types.d.ts +50 -0
- package/dist/types/pii-types.d.ts.map +1 -0
- package/dist/types/pii-types.js +114 -0
- package/dist/types/pii-types.js.map +1 -0
- package/dist/utils/iban-checksum.d.ts +23 -0
- package/dist/utils/iban-checksum.d.ts.map +1 -0
- package/dist/utils/iban-checksum.js +106 -0
- package/dist/utils/iban-checksum.js.map +1 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +10 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/luhn.d.ts +17 -0
- package/dist/utils/luhn.d.ts.map +1 -0
- package/dist/utils/luhn.js +55 -0
- package/dist/utils/luhn.js.map +1 -0
- package/dist/utils/offsets.d.ts +86 -0
- package/dist/utils/offsets.d.ts.map +1 -0
- package/dist/utils/offsets.js +124 -0
- package/dist/utils/offsets.js.map +1 -0
- package/dist/utils/path.d.ts +34 -0
- package/dist/utils/path.d.ts.map +1 -0
- package/dist/utils/path.js +96 -0
- package/dist/utils/path.js.map +1 -0
- package/dist/utils/storage-browser.d.ts +51 -0
- package/dist/utils/storage-browser.d.ts.map +1 -0
- package/dist/utils/storage-browser.js +381 -0
- package/dist/utils/storage-browser.js.map +1 -0
- package/dist/utils/storage-node.d.ts +43 -0
- package/dist/utils/storage-node.d.ts.map +1 -0
- package/dist/utils/storage-node.js +93 -0
- package/dist/utils/storage-node.js.map +1 -0
- package/dist/utils/storage.d.ts +70 -0
- package/dist/utils/storage.d.ts.map +1 -0
- package/dist/utils/storage.js +69 -0
- package/dist/utils/storage.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PII Map Encryption
|
|
3
|
+
* AES-256-GCM encryption for the PII mapping
|
|
4
|
+
* Uses Web Crypto API for browser compatibility
|
|
5
|
+
*/
|
|
6
|
+
import { EncryptedPIIMap } from "../types/index.js";
|
|
7
|
+
import type { RawPIIMap } from "../pipeline/tagger.js";
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Uint8Array to a Base64 string
|
|
10
|
+
*/
|
|
11
|
+
export declare function uint8ArrayToBase64(bytes: Uint8Array): string;
|
|
12
|
+
/**
|
|
13
|
+
* Converts a Base64 string to a Uint8Array
|
|
14
|
+
*/
|
|
15
|
+
export declare function base64ToUint8Array(base64: string): Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* Encryption configuration
|
|
18
|
+
*/
|
|
19
|
+
export interface EncryptionConfig {
|
|
20
|
+
/** IV length in bytes (default: 12 for GCM) */
|
|
21
|
+
ivLength: number;
|
|
22
|
+
/** Auth tag length in bits (default: 128) */
|
|
23
|
+
authTagLength: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Default encryption configuration
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULT_ENCRYPTION_CONFIG: EncryptionConfig;
|
|
29
|
+
/**
|
|
30
|
+
* Key generation options
|
|
31
|
+
*/
|
|
32
|
+
export interface KeyGenOptions {
|
|
33
|
+
/** Key length in bytes (default: 32 for AES-256) */
|
|
34
|
+
length: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Generates a random encryption key
|
|
38
|
+
* @returns Promise resolving to Uint8Array containing the key
|
|
39
|
+
*/
|
|
40
|
+
export declare function generateKey(options?: Partial<KeyGenOptions>): Uint8Array;
|
|
41
|
+
/**
|
|
42
|
+
* Generates a random salt for key derivation
|
|
43
|
+
* @param length - Salt length in bytes (default: 16)
|
|
44
|
+
* @returns Uint8Array containing the salt
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateSalt(length?: number): Uint8Array;
|
|
47
|
+
/**
|
|
48
|
+
* Derives a key from a password using PBKDF2
|
|
49
|
+
* @param password - Password string
|
|
50
|
+
* @param salt - Salt Uint8Array (should be randomly generated and stored)
|
|
51
|
+
* @param iterations - Number of iterations (default: 100000)
|
|
52
|
+
* @returns Promise resolving to Uint8Array containing the derived key
|
|
53
|
+
*/
|
|
54
|
+
export declare function deriveKey(password: string, salt: Uint8Array, iterations?: number): Promise<Uint8Array>;
|
|
55
|
+
/**
|
|
56
|
+
* Encrypts a PII map using AES-256-GCM
|
|
57
|
+
* @param piiMap - Raw PII map to encrypt
|
|
58
|
+
* @param key - 32-byte encryption key as Uint8Array
|
|
59
|
+
* @param config - Encryption configuration
|
|
60
|
+
* @returns Promise resolving to encrypted PII map
|
|
61
|
+
*/
|
|
62
|
+
export declare function encryptPIIMap(piiMap: RawPIIMap, key: Uint8Array, config?: Partial<EncryptionConfig>): Promise<EncryptedPIIMap>;
|
|
63
|
+
/**
|
|
64
|
+
* Decrypts an encrypted PII map
|
|
65
|
+
* @param encrypted - Encrypted PII map
|
|
66
|
+
* @param key - 32-byte encryption key as Uint8Array
|
|
67
|
+
* @param config - Encryption configuration
|
|
68
|
+
* @returns Promise resolving to decrypted PII map
|
|
69
|
+
*/
|
|
70
|
+
export declare function decryptPIIMap(encrypted: EncryptedPIIMap, key: Uint8Array, config?: Partial<EncryptionConfig>): Promise<RawPIIMap>;
|
|
71
|
+
/**
|
|
72
|
+
* Key provider interface for external key management
|
|
73
|
+
*/
|
|
74
|
+
export interface KeyProvider {
|
|
75
|
+
/** Gets the current encryption key */
|
|
76
|
+
getKey(): Promise<Uint8Array>;
|
|
77
|
+
/** Rotates to a new key (optional) */
|
|
78
|
+
rotateKey?(): Promise<Uint8Array>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Simple in-memory key provider (for testing/development)
|
|
82
|
+
* WARNING: Not secure for production use
|
|
83
|
+
*/
|
|
84
|
+
export declare class InMemoryKeyProvider implements KeyProvider {
|
|
85
|
+
private key;
|
|
86
|
+
private initialKey?;
|
|
87
|
+
constructor(key?: Uint8Array);
|
|
88
|
+
getKey(): Promise<Uint8Array>;
|
|
89
|
+
rotateKey(): Promise<Uint8Array>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Configuration-based key provider
|
|
93
|
+
* Accepts the key at construction time (platform-agnostic)
|
|
94
|
+
* Consumer is responsible for reading the key from environment variables or config
|
|
95
|
+
*/
|
|
96
|
+
export declare class ConfigKeyProvider implements KeyProvider {
|
|
97
|
+
private key;
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new ConfigKeyProvider
|
|
100
|
+
* @param keyBase64 - Base64-encoded 32-byte encryption key
|
|
101
|
+
*/
|
|
102
|
+
constructor(keyBase64: string);
|
|
103
|
+
getKey(): Promise<Uint8Array>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Validates that a key is suitable for AES-256
|
|
107
|
+
*/
|
|
108
|
+
export declare function validateKey(key: Uint8Array): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Securely compares two Uint8Arrays (constant-time)
|
|
111
|
+
* Prevents timing attacks by always comparing all bytes
|
|
112
|
+
*/
|
|
113
|
+
export declare function secureCompare(a: Uint8Array, b: Uint8Array): boolean;
|
|
114
|
+
//# sourceMappingURL=pii-map-crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pii-map-crypto.d.ts","sourceRoot":"","sources":["../../src/crypto/pii-map-crypto.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAMvD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAG5D;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAG7D;AAMD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+CAA+C;IAC/C,QAAQ,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,eAAO,MAAM,yBAAyB,EAAE,gBAGvC,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,oDAAoD;IACpD,MAAM,EAAE,MAAM,CAAC;CAChB;AAMD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GAAG,UAAU,CAK5E;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MA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,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CASnE"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PII Map Encryption
|
|
3
|
+
* AES-256-GCM encryption for the PII mapping
|
|
4
|
+
* Uses Web Crypto API for browser compatibility
|
|
5
|
+
*/
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Base64 Utility Functions
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Uint8Array to a Base64 string
|
|
11
|
+
*/
|
|
12
|
+
export function uint8ArrayToBase64(bytes) {
|
|
13
|
+
const binString = Array.from(bytes, (b) => String.fromCodePoint(b)).join("");
|
|
14
|
+
return btoa(binString);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Converts a Base64 string to a Uint8Array
|
|
18
|
+
*/
|
|
19
|
+
export function base64ToUint8Array(base64) {
|
|
20
|
+
const binString = atob(base64);
|
|
21
|
+
return Uint8Array.from(binString, (c) => c.codePointAt(0));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Default encryption configuration
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULT_ENCRYPTION_CONFIG = {
|
|
27
|
+
ivLength: 12,
|
|
28
|
+
authTagLength: 128, // Web Crypto uses bits, not bytes
|
|
29
|
+
};
|
|
30
|
+
// ============================================================================
|
|
31
|
+
// Core Crypto Functions
|
|
32
|
+
// ============================================================================
|
|
33
|
+
/**
|
|
34
|
+
* Generates a random encryption key
|
|
35
|
+
* @returns Promise resolving to Uint8Array containing the key
|
|
36
|
+
*/
|
|
37
|
+
export function generateKey(options = {}) {
|
|
38
|
+
const length = options.length ?? 32;
|
|
39
|
+
const key = new Uint8Array(length);
|
|
40
|
+
globalThis.crypto.getRandomValues(key);
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Generates a random salt for key derivation
|
|
45
|
+
* @param length - Salt length in bytes (default: 16)
|
|
46
|
+
* @returns Uint8Array containing the salt
|
|
47
|
+
*/
|
|
48
|
+
export function generateSalt(length = 16) {
|
|
49
|
+
const salt = new Uint8Array(length);
|
|
50
|
+
globalThis.crypto.getRandomValues(salt);
|
|
51
|
+
return salt;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Derives a key from a password using PBKDF2
|
|
55
|
+
* @param password - Password string
|
|
56
|
+
* @param salt - Salt Uint8Array (should be randomly generated and stored)
|
|
57
|
+
* @param iterations - Number of iterations (default: 100000)
|
|
58
|
+
* @returns Promise resolving to Uint8Array containing the derived key
|
|
59
|
+
*/
|
|
60
|
+
export async function deriveKey(password, salt, iterations = 100000) {
|
|
61
|
+
const encoder = new TextEncoder();
|
|
62
|
+
const passwordBuffer = encoder.encode(password);
|
|
63
|
+
// Import password as a key
|
|
64
|
+
const baseKey = await globalThis.crypto.subtle.importKey("raw", passwordBuffer, "PBKDF2", false, ["deriveBits"]);
|
|
65
|
+
// Derive bits using PBKDF2
|
|
66
|
+
const derivedBits = await globalThis.crypto.subtle.deriveBits({
|
|
67
|
+
name: "PBKDF2",
|
|
68
|
+
salt: salt,
|
|
69
|
+
iterations: iterations,
|
|
70
|
+
hash: "SHA-256",
|
|
71
|
+
}, baseKey, 256 // 32 bytes * 8 bits
|
|
72
|
+
);
|
|
73
|
+
return new Uint8Array(derivedBits);
|
|
74
|
+
}
|
|
75
|
+
// ============================================================================
|
|
76
|
+
// Encrypt / Decrypt Functions
|
|
77
|
+
// ============================================================================
|
|
78
|
+
/**
|
|
79
|
+
* Encrypts a PII map using AES-256-GCM
|
|
80
|
+
* @param piiMap - Raw PII map to encrypt
|
|
81
|
+
* @param key - 32-byte encryption key as Uint8Array
|
|
82
|
+
* @param config - Encryption configuration
|
|
83
|
+
* @returns Promise resolving to encrypted PII map
|
|
84
|
+
*/
|
|
85
|
+
export async function encryptPIIMap(piiMap, key, config = {}) {
|
|
86
|
+
const encConfig = { ...DEFAULT_ENCRYPTION_CONFIG, ...config };
|
|
87
|
+
// Validate key length
|
|
88
|
+
if (key.length !== 32) {
|
|
89
|
+
throw new Error(`Invalid key length: expected 32 bytes, got ${key.length}`);
|
|
90
|
+
}
|
|
91
|
+
// Convert map to JSON
|
|
92
|
+
const mapObject = {};
|
|
93
|
+
for (const [k, v] of piiMap) {
|
|
94
|
+
mapObject[k] = v;
|
|
95
|
+
}
|
|
96
|
+
const plaintext = JSON.stringify(mapObject);
|
|
97
|
+
// Generate random IV
|
|
98
|
+
const iv = new Uint8Array(encConfig.ivLength);
|
|
99
|
+
globalThis.crypto.getRandomValues(iv);
|
|
100
|
+
// Import key for AES-GCM
|
|
101
|
+
const cryptoKey = await globalThis.crypto.subtle.importKey("raw", key, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
102
|
+
// Encrypt using AES-GCM
|
|
103
|
+
const encoder = new TextEncoder();
|
|
104
|
+
const plaintextBuffer = encoder.encode(plaintext);
|
|
105
|
+
const encryptedBuffer = await globalThis.crypto.subtle.encrypt({
|
|
106
|
+
name: "AES-GCM",
|
|
107
|
+
iv: iv,
|
|
108
|
+
tagLength: encConfig.authTagLength,
|
|
109
|
+
}, cryptoKey, plaintextBuffer);
|
|
110
|
+
// Web Crypto returns ciphertext + authTag concatenated
|
|
111
|
+
const encryptedArray = new Uint8Array(encryptedBuffer);
|
|
112
|
+
const authTagBytes = encConfig.authTagLength / 8;
|
|
113
|
+
const ciphertext = encryptedArray.slice(0, encryptedArray.length - authTagBytes);
|
|
114
|
+
const authTag = encryptedArray.slice(encryptedArray.length - authTagBytes);
|
|
115
|
+
return {
|
|
116
|
+
ciphertext: uint8ArrayToBase64(ciphertext),
|
|
117
|
+
iv: uint8ArrayToBase64(iv),
|
|
118
|
+
authTag: uint8ArrayToBase64(authTag),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Decrypts an encrypted PII map
|
|
123
|
+
* @param encrypted - Encrypted PII map
|
|
124
|
+
* @param key - 32-byte encryption key as Uint8Array
|
|
125
|
+
* @param config - Encryption configuration
|
|
126
|
+
* @returns Promise resolving to decrypted PII map
|
|
127
|
+
*/
|
|
128
|
+
export async function decryptPIIMap(encrypted, key, config = {}) {
|
|
129
|
+
const encConfig = { ...DEFAULT_ENCRYPTION_CONFIG, ...config };
|
|
130
|
+
// Validate key length
|
|
131
|
+
if (key.length !== 32) {
|
|
132
|
+
throw new Error(`Invalid key length: expected 32 bytes, got ${key.length}`);
|
|
133
|
+
}
|
|
134
|
+
// Decode base64
|
|
135
|
+
const ciphertext = base64ToUint8Array(encrypted.ciphertext);
|
|
136
|
+
const iv = base64ToUint8Array(encrypted.iv);
|
|
137
|
+
const authTag = base64ToUint8Array(encrypted.authTag);
|
|
138
|
+
// Web Crypto expects ciphertext + authTag concatenated
|
|
139
|
+
const encryptedData = new Uint8Array(ciphertext.length + authTag.length);
|
|
140
|
+
encryptedData.set(ciphertext, 0);
|
|
141
|
+
encryptedData.set(authTag, ciphertext.length);
|
|
142
|
+
// Import key for AES-GCM
|
|
143
|
+
const cryptoKey = await globalThis.crypto.subtle.importKey("raw", key, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
144
|
+
// Decrypt using AES-GCM
|
|
145
|
+
const decryptedBuffer = await globalThis.crypto.subtle.decrypt({
|
|
146
|
+
name: "AES-GCM",
|
|
147
|
+
iv: iv,
|
|
148
|
+
tagLength: encConfig.authTagLength,
|
|
149
|
+
}, cryptoKey, encryptedData);
|
|
150
|
+
// Parse JSON back to map
|
|
151
|
+
const decoder = new TextDecoder();
|
|
152
|
+
const decryptedText = decoder.decode(decryptedBuffer);
|
|
153
|
+
const mapObject = JSON.parse(decryptedText);
|
|
154
|
+
const piiMap = new Map();
|
|
155
|
+
for (const [k, v] of Object.entries(mapObject)) {
|
|
156
|
+
piiMap.set(k, v);
|
|
157
|
+
}
|
|
158
|
+
return piiMap;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Simple in-memory key provider (for testing/development)
|
|
162
|
+
* WARNING: Not secure for production use
|
|
163
|
+
*/
|
|
164
|
+
export class InMemoryKeyProvider {
|
|
165
|
+
key = null;
|
|
166
|
+
initialKey;
|
|
167
|
+
constructor(key) {
|
|
168
|
+
this.initialKey = key;
|
|
169
|
+
}
|
|
170
|
+
getKey() {
|
|
171
|
+
if (this.key === null) {
|
|
172
|
+
this.key = this.initialKey ?? generateKey();
|
|
173
|
+
}
|
|
174
|
+
return Promise.resolve(this.key);
|
|
175
|
+
}
|
|
176
|
+
rotateKey() {
|
|
177
|
+
this.key = generateKey();
|
|
178
|
+
return Promise.resolve(this.key);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Configuration-based key provider
|
|
183
|
+
* Accepts the key at construction time (platform-agnostic)
|
|
184
|
+
* Consumer is responsible for reading the key from environment variables or config
|
|
185
|
+
*/
|
|
186
|
+
export class ConfigKeyProvider {
|
|
187
|
+
key;
|
|
188
|
+
/**
|
|
189
|
+
* Creates a new ConfigKeyProvider
|
|
190
|
+
* @param keyBase64 - Base64-encoded 32-byte encryption key
|
|
191
|
+
*/
|
|
192
|
+
constructor(keyBase64) {
|
|
193
|
+
if (!keyBase64 || keyBase64.length === 0) {
|
|
194
|
+
throw new Error("Encryption key must be provided");
|
|
195
|
+
}
|
|
196
|
+
this.key = base64ToUint8Array(keyBase64);
|
|
197
|
+
if (this.key.length !== 32) {
|
|
198
|
+
throw new Error(`Invalid key length: expected 32 bytes, got ${this.key.length}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
getKey() {
|
|
202
|
+
return Promise.resolve(this.key);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// ============================================================================
|
|
206
|
+
// Utility Functions
|
|
207
|
+
// ============================================================================
|
|
208
|
+
/**
|
|
209
|
+
* Validates that a key is suitable for AES-256
|
|
210
|
+
*/
|
|
211
|
+
export function validateKey(key) {
|
|
212
|
+
return key.length === 32;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Securely compares two Uint8Arrays (constant-time)
|
|
216
|
+
* Prevents timing attacks by always comparing all bytes
|
|
217
|
+
*/
|
|
218
|
+
export function secureCompare(a, b) {
|
|
219
|
+
if (a.length !== b.length) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
let result = 0;
|
|
223
|
+
for (let i = 0; i < a.length; i++) {
|
|
224
|
+
result |= a[i] ^ b[i];
|
|
225
|
+
}
|
|
226
|
+
return result === 0;
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=pii-map-crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pii-map-crypto.js","sourceRoot":"","sources":["../../src/crypto/pii-map-crypto.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,+EAA+E;AAC/E,2BAA2B;AAC3B,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAiB;IAClD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7E,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC;AACzB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAC/C,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAC/B,OAAO,UAAU,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAE,CAAC,CAAC;AAC9D,CAAC;AAgBD;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAqB;IACzD,QAAQ,EAAE,EAAE;IACZ,aAAa,EAAE,GAAG,EAAE,kCAAkC;CACvD,CAAC;AAUF,+EAA+E;AAC/E,wBAAwB;AACxB,+EAA+E;AAE/E;;;GAGG;AAC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C;QAClC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,CAAC;IAC1B,CAAC;IACD,OAAO,MAAM,KAAK,CAAC,CAAC;AACtB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rehydra Module
|
|
3
|
+
* Main entry point for on-device PII anonymization
|
|
4
|
+
*/
|
|
5
|
+
export * from "./types/index.js";
|
|
6
|
+
export { Recognizer, RegexRecognizer, RecognizerRegistry, createDefaultRegistry, createRegistry, getGlobalRegistry, emailRecognizer, phoneRecognizer, ibanRecognizer, bicSwiftRecognizer, creditCardRecognizer, ipAddressRecognizer, urlRecognizer, createCustomIdRecognizer, createCaseIdRecognizer, createCustomerIdRecognizer, } from "./recognizers/index.js";
|
|
7
|
+
export { NERModel, NERModelStub, createNERModel, createNERModelStub, WordPieceTokenizer, loadVocabFromFile, parseVocab, loadRuntime, detectRuntime, getRuntimeType, type INERModel, type NERModelConfig, type NERPrediction, type NERModelMode, type DownloadProgressCallback, MODEL_REGISTRY, getModelCacheDir, isModelDownloaded, downloadModel, ensureModel, clearModelCache, listDownloadedModels, } from "./ner/index.js";
|
|
8
|
+
export { prenormalize, resolveEntities, tagEntities, validateOutput, generateTag, parseTag, rehydrate, enrichSemantics, inferGender, classifyLocation, getDatabaseStats, hasName, hasLocation, isSemanticDataAvailable, isSemanticDataDownloaded, getSemanticDataCacheDir, getDataDirectory, downloadSemanticData, ensureSemanticData, initializeSemanticData, loadSemanticData, clearSemanticData, clearSemanticDataCache, getSemanticDataInfo, SEMANTIC_DATA_FILES, extractTitle, extractTitlesFromSpans, mergeAdjacentTitleSpans, getTitlesForLanguage, getAllTitles, startsWithTitle, isOnlyTitle, type SemanticDataFileInfo, type EnricherConfig, type GenderResult, type LocationResult, type TitleExtractionResult, } from "./pipeline/index.js";
|
|
9
|
+
export { encryptPIIMap, decryptPIIMap, generateKey, deriveKey, generateSalt, KeyProvider, InMemoryKeyProvider, ConfigKeyProvider, validateKey, secureCompare, uint8ArrayToBase64, base64ToUint8Array, } from "./crypto/index.js";
|
|
10
|
+
export { getStorageProvider, isNode, isBrowser, resetStorageProvider, setStorageProvider, type StorageProvider, } from "./utils/storage.js";
|
|
11
|
+
export { join as pathJoin, dirname as pathDirname, basename as pathBasename, normalize as pathNormalize, extname as pathExtname, isAbsolute as pathIsAbsolute, } from "./utils/path.js";
|
|
12
|
+
import { AnonymizationResult, AnonymizationPolicy, SemanticConfig, PIIType } from "./types/index.js";
|
|
13
|
+
import { RecognizerRegistry } from "./recognizers/index.js";
|
|
14
|
+
import { type INERModel } from "./ner/index.js";
|
|
15
|
+
import { type NERModelMode, type DownloadProgressCallback } from "./ner/model-manager.js";
|
|
16
|
+
import { type KeyProvider } from "./crypto/index.js";
|
|
17
|
+
/**
|
|
18
|
+
* NER configuration options
|
|
19
|
+
*/
|
|
20
|
+
export interface NERConfig {
|
|
21
|
+
/**
|
|
22
|
+
* NER model mode:
|
|
23
|
+
* - 'standard': Full-size multilingual model (~1.1 GB)
|
|
24
|
+
* - 'quantized': Smaller quantized model (~280 MB)
|
|
25
|
+
* - 'disabled': No NER, regex-only detection
|
|
26
|
+
* - 'custom': Use custom model paths
|
|
27
|
+
*/
|
|
28
|
+
mode: NERModelMode;
|
|
29
|
+
/**
|
|
30
|
+
* Custom model path (required when mode is 'custom')
|
|
31
|
+
*/
|
|
32
|
+
modelPath?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Custom vocab path (required when mode is 'custom')
|
|
35
|
+
*/
|
|
36
|
+
vocabPath?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to auto-download model if not present
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
autoDownload?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Callback for download progress
|
|
44
|
+
*/
|
|
45
|
+
onDownloadProgress?: DownloadProgressCallback;
|
|
46
|
+
/**
|
|
47
|
+
* Callback for status messages
|
|
48
|
+
*/
|
|
49
|
+
onStatus?: (status: string) => void;
|
|
50
|
+
/**
|
|
51
|
+
* Confidence thresholds per PII type (0.0 - 1.0)
|
|
52
|
+
* Overrides default thresholds for specified types
|
|
53
|
+
* @example { PERSON: 0.8, ORG: 0.7 }
|
|
54
|
+
*/
|
|
55
|
+
thresholds?: Partial<Record<PIIType, number>>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Anonymizer configuration
|
|
59
|
+
*/
|
|
60
|
+
export interface AnonymizerConfig {
|
|
61
|
+
/** Recognizer registry (uses default if not provided) */
|
|
62
|
+
registry?: RecognizerRegistry;
|
|
63
|
+
/**
|
|
64
|
+
* NER configuration
|
|
65
|
+
* @default { mode: 'disabled' }
|
|
66
|
+
*/
|
|
67
|
+
ner?: NERConfig;
|
|
68
|
+
/**
|
|
69
|
+
* Semantic enrichment configuration
|
|
70
|
+
* Enables MT-friendly PII tags with gender/scope attributes
|
|
71
|
+
* @default { enabled: false }
|
|
72
|
+
*/
|
|
73
|
+
semantic?: SemanticConfig;
|
|
74
|
+
/** Key provider for encryption (generates random key if not provided) */
|
|
75
|
+
keyProvider?: KeyProvider;
|
|
76
|
+
/** Default policy (uses default if not provided) */
|
|
77
|
+
defaultPolicy?: AnonymizationPolicy;
|
|
78
|
+
/** Model version string */
|
|
79
|
+
modelVersion?: string;
|
|
80
|
+
/** Policy version string */
|
|
81
|
+
policyVersion?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Anonymizer instance
|
|
85
|
+
* Main class for performing PII anonymization
|
|
86
|
+
*/
|
|
87
|
+
export declare class Anonymizer {
|
|
88
|
+
private registry;
|
|
89
|
+
private nerModel;
|
|
90
|
+
private nerConfig;
|
|
91
|
+
private semanticConfig;
|
|
92
|
+
private keyProvider;
|
|
93
|
+
private defaultPolicy;
|
|
94
|
+
private modelVersion;
|
|
95
|
+
private policyVersion;
|
|
96
|
+
private initialized;
|
|
97
|
+
private semanticDataReady;
|
|
98
|
+
constructor(config?: AnonymizerConfig);
|
|
99
|
+
/**
|
|
100
|
+
* Initializes the anonymizer
|
|
101
|
+
* Downloads NER model and semantic data if needed and loads them
|
|
102
|
+
*/
|
|
103
|
+
initialize(): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Anonymizes text, replacing PII with placeholder tags
|
|
106
|
+
* @param text - Input text to anonymize
|
|
107
|
+
* @param locale - Optional locale hint (e.g., 'de-DE', 'en-US')
|
|
108
|
+
* @param policy - Optional policy override
|
|
109
|
+
* @returns Anonymization result with anonymized text and encrypted PII map
|
|
110
|
+
*/
|
|
111
|
+
anonymize(text: string, locale?: string, policy?: Partial<AnonymizationPolicy>): Promise<AnonymizationResult>;
|
|
112
|
+
/**
|
|
113
|
+
* Disposes of resources
|
|
114
|
+
*/
|
|
115
|
+
dispose(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Gets the recognizer registry
|
|
118
|
+
*/
|
|
119
|
+
getRegistry(): RecognizerRegistry;
|
|
120
|
+
/**
|
|
121
|
+
* Gets the NER model
|
|
122
|
+
*/
|
|
123
|
+
getNERModel(): INERModel | null;
|
|
124
|
+
/**
|
|
125
|
+
* Whether the anonymizer is initialized
|
|
126
|
+
*/
|
|
127
|
+
get isInitialized(): boolean;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Creates an anonymizer with the specified configuration
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* // Regex-only (no NER)
|
|
135
|
+
* const anonymizer = createAnonymizer();
|
|
136
|
+
*
|
|
137
|
+
* // With NER (auto-downloads model on first use)
|
|
138
|
+
* const anonymizer = createAnonymizer({
|
|
139
|
+
* ner: { mode: 'quantized' }
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* // With NER and progress callback
|
|
143
|
+
* const anonymizer = createAnonymizer({
|
|
144
|
+
* ner: {
|
|
145
|
+
* mode: 'standard',
|
|
146
|
+
* onStatus: (status) => console.log(status),
|
|
147
|
+
* onDownloadProgress: (p) => console.log(`${p.file}: ${p.percent}%`)
|
|
148
|
+
* }
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function createAnonymizer(config?: AnonymizerConfig): Anonymizer;
|
|
153
|
+
/**
|
|
154
|
+
* Convenience function for one-off anonymization
|
|
155
|
+
* Creates a temporary anonymizer with default settings (regex-only)
|
|
156
|
+
*/
|
|
157
|
+
export declare function anonymize(text: string, locale?: string, policy?: Partial<AnonymizationPolicy>): Promise<AnonymizationResult>;
|
|
158
|
+
/**
|
|
159
|
+
* Quick regex-only anonymization (no NER, faster)
|
|
160
|
+
*/
|
|
161
|
+
export declare function anonymizeRegexOnly(text: string, policy?: Partial<AnonymizationPolicy>): Promise<AnonymizationResult>;
|
|
162
|
+
/**
|
|
163
|
+
* Full anonymization with NER
|
|
164
|
+
* Auto-downloads the quantized model on first use
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const result = await anonymizeWithNER(
|
|
169
|
+
* 'Contact John Smith at john@example.com',
|
|
170
|
+
* {
|
|
171
|
+
* mode: 'quantized',
|
|
172
|
+
* onStatus: console.log
|
|
173
|
+
* }
|
|
174
|
+
* );
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export declare function anonymizeWithNER(text: string, nerConfig: Omit<NERConfig, "mode"> & {
|
|
178
|
+
mode?: "standard" | "quantized";
|
|
179
|
+
}, policy?: Partial<AnonymizationPolicy>): Promise<AnonymizationResult>;
|
|
180
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,cAAc,kBAAkB,CAAC;AAGjC,OAAO,EACL,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,qBAAqB,EACrB,cAAc,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