const {
makeGlobalKeys,
makeSessionKeys,
PEPJSONBuilder,
encryptJson,
decryptJson,
transcryptJsonBatch,
TranscryptionInfo,
PseudonymizationSecret,
EncryptionSecret,
PseudonymizationDomain,
EncryptionContext, encryptJsonBatch,
} = require("../../pkg/libpep.js");
test('test json transcryption with builder', async () => {
const globalKeys = makeGlobalKeys();
const pseudoSecret = new PseudonymizationSecret(Uint8Array.from(Buffer.from("pseudo-secret")));
const encSecret = new EncryptionSecret(Uint8Array.from(Buffer.from("encryption-secret")));
const session = new EncryptionContext("session-1");
const sessionKeys = makeSessionKeys(globalKeys.secret, session, encSecret);
const patientData = {
user_id: "user-67890",
name: "Alice",
age: 30,
active: true
};
const patientRecord = PEPJSONBuilder.fromJson(patientData, ["user_id"]).build();
const encrypted = encryptJson(patientRecord, sessionKeys);
const decryptedOriginal = decryptJson(encrypted, sessionKeys);
const jsonOriginal = decryptedOriginal.toJson();
expect(jsonOriginal.get("user_id")).toBe("user-67890");
expect(jsonOriginal.get("name")).toBe("Alice");
expect(jsonOriginal.get("age")).toBe(30);
expect(jsonOriginal.get("active")).toBe(true);
const domainA = new PseudonymizationDomain("clinic-a");
const domainB = new PseudonymizationDomain("clinic-b");
const transcrypted = encrypted.transcrypt(
domainA,
domainB,
session,
session,
pseudoSecret,
encSecret
);
expect(encrypted).not.toBe(transcrypted);
const decryptedTranscrypted = decryptJson(transcrypted, sessionKeys);
const jsonTranscrypted = decryptedTranscrypted.toJson();
expect(jsonTranscrypted.get("name")).toBe("Alice");
expect(jsonTranscrypted.get("age")).toBe(30);
expect(jsonTranscrypted.get("active")).toBe(true);
expect(jsonTranscrypted.get("user_id")).not.toBe("user-67890");
});
test('test json batch transcryption same structure', async () => {
const globalKeys = makeGlobalKeys();
const pseudoSecret = new PseudonymizationSecret(Uint8Array.from(Buffer.from("pseudo-secret")));
const encSecret = new EncryptionSecret(Uint8Array.from(Buffer.from("encryption-secret")));
const domainA = new PseudonymizationDomain("domain-a");
const domainB = new PseudonymizationDomain("domain-b");
const session = new EncryptionContext("session-1");
const sessionKeys = makeSessionKeys(globalKeys.secret, session, encSecret);
const data1 = {
patient_id: "patient-001",
diagnosis: "Flu",
temperature: 38.5
};
const data2 = {
patient_id: "patient-002",
diagnosis: "Cold",
temperature: 37.2
};
const record1 = PEPJSONBuilder.fromJson(data1, ["patient_id"]).build();
const record2 = PEPJSONBuilder.fromJson(data2, ["patient_id"]).build();
const encrypted1 = encryptJson(record1, sessionKeys);
const encrypted2 = encryptJson(record2, sessionKeys);
const structure1 = encrypted1.structure();
const structure2 = encrypted2.structure();
expect(structure1.equals(structure2)).toBe(true);
const transcryptionInfo = new TranscryptionInfo(
domainA,
domainB,
session,
session,
pseudoSecret,
encSecret
);
const transcryptedBatch = transcryptJsonBatch(
[encrypted1, encrypted2],
transcryptionInfo
);
expect(transcryptedBatch.length).toBe(2);
expect(transcryptedBatch[0]).not.toBe(encrypted1);
expect(transcryptedBatch[1]).not.toBe(encrypted2);
const decryptedBatch = transcryptedBatch.map(v => decryptJson(v, sessionKeys).toJson());
decryptedBatch.sort((a, b) => a.get("temperature") - b.get("temperature"));
expect(decryptedBatch[0].get("diagnosis")).toBe("Cold");
expect(decryptedBatch[0].get("temperature")).toBe(37.2);
expect(decryptedBatch[0].get("patient_id")).not.toBe("patient-002");
expect(decryptedBatch[1].get("diagnosis")).toBe("Flu");
expect(decryptedBatch[1].get("temperature")).toBe(38.5);
expect(decryptedBatch[1].get("patient_id")).not.toBe("patient-001");
});
test('test json batch transcryption different structures', async () => {
const globalKeys = makeGlobalKeys();
const pseudoSecret = new PseudonymizationSecret(Uint8Array.from(Buffer.from("pseudo-secret")));
const encSecret = new EncryptionSecret(Uint8Array.from(Buffer.from("encryption-secret")));
const domainA = new PseudonymizationDomain("domain-a");
const domainB = new PseudonymizationDomain("domain-b");
const session = new EncryptionContext("session-1");
const sessionKeys = makeSessionKeys(globalKeys.secret, session, encSecret);
const data1 = {
patient_id: "patient-001",
diagnosis: "Flu",
temperature: 38.5
};
const data2 = {
user_id: "user-002",
name: "Bob",
age: 25,
active: true
};
const record1 = PEPJSONBuilder.fromJson(data1, ["patient_id"]).build();
const record2 = PEPJSONBuilder.fromJson(data2, ["user_id"]).build();
const encrypted1 = encryptJson(record1, sessionKeys);
const encrypted2 = encryptJson(record2, sessionKeys);
const structure1 = encrypted1.structure();
const structure2 = encrypted2.structure();
expect(structure1.equals(structure2)).toBe(false);
const transcryptionInfo = new TranscryptionInfo(
domainA,
domainB,
session,
session,
pseudoSecret,
encSecret
);
expect(() => {
transcryptJsonBatch([encrypted1, encrypted2], transcryptionInfo);
}).toThrow(/Inconsistent structure in batch/);
});
test('test json batch transcryption same structure different lengths', async () => {
const globalKeys = makeGlobalKeys();
const pseudoSecret = new PseudonymizationSecret(Uint8Array.from(Buffer.from("pseudo-secret")));
const encSecret = new EncryptionSecret(Uint8Array.from(Buffer.from("encryption-secret")));
const domainA = new PseudonymizationDomain("domain-a");
const domainB = new PseudonymizationDomain("domain-b");
const session = new EncryptionContext("session-1");
const sessionKeys = makeSessionKeys(globalKeys.secret, session, encSecret);
const data1 = {
patient_id: "patient-001",
diagnosis: "Flu",
temperature: 38.5
};
const data2 = {
patient_id: "patient-002 with a very long ID that makes the structure different",
diagnosis: "Flu but with very long description that makes the structure different",
temperature: 38.5
};
const record1 = PEPJSONBuilder.fromJson(data1, ["patient_id"]).build();
const record2 = PEPJSONBuilder.fromJson(data2, ["patient_id"]).build();
const encrypted1 = encryptJson(record1, sessionKeys);
const encrypted2 = encryptJson(record2, sessionKeys);
const structure1 = encrypted1.structure();
const structure2 = encrypted2.structure();
expect(structure1.equals(structure2)).toBe(false);
const transcryptionInfo = new TranscryptionInfo(
domainA,
domainB,
session,
session,
pseudoSecret,
encSecret
);
expect(() => {
transcryptJsonBatch([encrypted1, encrypted2], transcryptionInfo);
}).toThrow(/Inconsistent structure in batch/);
const encryptedBatch = encryptJsonBatch([record1, record2], sessionKeys);
const transcryptedBatch = transcryptJsonBatch(encryptedBatch, transcryptionInfo);
expect(transcryptedBatch.length).toBe(2);
});