To restore original values, decrypt the PII map and rehydrate:
Copy
import { decryptPIIMap, rehydrate } from 'rehydra';// Get the key (must be the same key used for encryption)const key = await keyProvider.getKey();// Decrypt the PII mapconst piiMap = await decryptPIIMap(result.piiMap, key);// Returns Map<string, string> where key is "EMAIL:1" and value is "[email protected]"// Replace placeholders with original valuesconst original = rehydrate(translatedText, piiMap);
import { deriveKey, generateSalt } from 'rehydra';const password = 'user-password';const salt = generateSalt(); // Store this with the encrypted dataconst key = await deriveKey(password, salt);
Always use the encrypted storage. Never log or expose the decrypted piiMap.
Copy
// ❌ Never do thisconsole.log(piiMap);// ✅ Safe to logconsole.log(result.piiMap); // Only encrypted data
Persist keys securely
Use platform-specific secure storage:
iOS: Keychain
Android: Keystore
Desktop: OS keychain (via keytar or similar)
Server: Environment variables, secrets manager
Implement key rotation
For long-running applications, rotate keys periodically:
Copy
// 1. Decrypt with old keyconst piiMap = await decryptPIIMap(oldEncrypted, oldKey);// 2. Re-encrypt with new keyconst newEncrypted = await encryptPIIMap(piiMap, newKey);// 3. Store new encrypted dataawait storage.save(sessionId, newEncrypted);