Rehydration is the process of replacing placeholder tags with original values.
rehydrate()
Replaces PII placeholders with original values.
Signature
function rehydrate(
text: string,
piiMap: Map<string, string>,
strict?: boolean,
tagFormat?: TagFormat
): string
Parameters
Text containing PII placeholder tags
piiMap
Map<string, string>
required
Decrypted PII map from decryptPIIMap()
If true, use strict matching (exact tag format only). If false, use fuzzy matching that handles mangled tags from translation.
Tag format configuration. Must match the format used during anonymization. Defaults to XML-style (<PII type="..." id="N"/>).
Returns
Text with placeholders replaced by original values.
Example
import { rehydrate, decryptPIIMap } from 'rehydra';
// After anonymization and translation
const translated = 'Hallo <PII type="PERSON" id="1"/> von <PII type="ORG" id="1"/>!';
// Decrypt the PII map
const key = await keyProvider.getKey();
const piiMap = await decryptPIIMap(result.piiMap, key);
// Map { 'PERSON:1' => 'John Smith', 'ORG:1' => 'Acme Corp' }
// Rehydrate
const original = rehydrate(translated, piiMap);
// "Hallo John Smith von Acme Corp!"
decryptPIIMap()
Decrypts the encrypted PII map from an anonymization result.
Signature
function decryptPIIMap(
encryptedMap: EncryptedPIIMap,
key: Uint8Array
): Promise<Map<string, string>>
Parameters
The encrypted PII map from result.piiMapinterface EncryptedPIIMap {
ciphertext: string; // Base64
iv: string; // Base64
authTag: string; // Base64
}
The encryption key (same key used during anonymization)
Returns
A Map<string, string> where:
- Key:
"TYPE:id" (e.g., "EMAIL:1", "PERSON:2")
- Value: Original PII value
Example
import { decryptPIIMap, InMemoryKeyProvider } from 'rehydra';
const keyProvider = new InMemoryKeyProvider();
// During anonymization
const anonymizer = createAnonymizer({ keyProvider });
const result = await anonymizer.anonymize('Email: john@example.com');
// Later, to rehydrate
const key = await keyProvider.getKey();
const piiMap = await decryptPIIMap(result.piiMap, key);
console.log(piiMap.get('EMAIL:1')); // "john@example.com"
Complete Workflow
import {
createAnonymizer,
decryptPIIMap,
rehydrate,
InMemoryKeyProvider
} from 'rehydra';
// 1. Setup
const keyProvider = new InMemoryKeyProvider();
const anonymizer = createAnonymizer({
ner: { mode: 'quantized' },
keyProvider
});
await anonymizer.initialize();
// 2. Anonymize
const result = await anonymizer.anonymize(
'Contact John Smith at john@acme.com'
);
console.log(result.anonymizedText);
// "Contact <PII type="PERSON" id="1"/> at <PII type="EMAIL" id="1"/>"
// 3. Process (e.g., translate)
const translated = await translate(result.anonymizedText);
// "Kontaktieren Sie <PII type="PERSON" id="1"/> unter <PII type="EMAIL" id="1"/>"
// 4. Decrypt PII map
const key = await keyProvider.getKey();
const piiMap = await decryptPIIMap(result.piiMap, key);
// 5. Rehydrate
const final = rehydrate(translated, piiMap);
console.log(final);
// "Kontaktieren Sie John Smith unter john@acme.com"
// 6. Cleanup
await anonymizer.dispose();
Session-Based Rehydration
When using sessions, rehydration is simplified:
const session = anonymizer.session('chat-123');
// Anonymize (auto-saves PII map)
const result = await session.anonymize('Hello John Smith!');
// Rehydrate (auto-loads and decrypts)
const original = await session.rehydrate(result.anonymizedText);
See Sessions for details.
By default, rehydrate expects placeholders in XML-style format:
<PII type="TYPE" id="N"/>
<PII type="TYPE" id="N" gender="..."/>
<PII type="TYPE" id="N" scope="..."/>
When using a custom tag format, pass the same tagFormat used during anonymization:
const tagFormat = { open: '[[', close: ']]' };
const text = '[[PII type="PERSON" id="1"]] from [[PII type="ORG" id="2"]]';
const result = rehydrate(text, piiMap, false, tagFormat);
The function matches based on type and id attributes.
const text = 'Hello <PII type="PERSON" id="1"/> and <BROKEN/>';
const result = rehydrate(text, piiMap);
// "Hello John Smith and <BROKEN/>"
// Unmatched tags are preserved
Error Handling
Missing Key
try {
const piiMap = await decryptPIIMap(result.piiMap, wrongKey);
} catch (error) {
// "Decryption failed: invalid key or corrupted data"
}
Missing PII Entry
const piiMap = new Map([['EMAIL:1', 'test@example.com']]);
const text = '<PII type="PERSON" id="1"/>'; // Not in map
const result = rehydrate(text, piiMap);
// "<PII type="PERSON" id="1"/>" // Unchanged (no match)
- Encryption - How PII maps are encrypted
- Sessions - Simplified rehydration with sessions
- Crypto - Key management utilities