feat: add hashing methods for email and IP in CryptoService for blind indexing

Introduced `hashEmail` and `hashIp` methods to enable searching on encrypted data. Added support to retrieve PGP encryption key from configuration.
This commit is contained in:
Mathis HERRIOT
2026-01-08 15:25:40 +01:00
parent 342e9b99da
commit fe309bc1e3

View File

@@ -34,6 +34,31 @@ export class CryptoService {
); );
} }
// --- Blind Indexing (for search on encrypted data) ---
async hashEmail(email: string): Promise<string> {
const normalizedEmail = email.toLowerCase().trim();
const data = new TextEncoder().encode(normalizedEmail);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(hashBuffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
async hashIp(ip: string): Promise<string> {
const data = new TextEncoder().encode(ip);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(hashBuffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
getPgpEncryptionKey(): string {
return (
this.configService.get<string>("PGP_ENCRYPTION_KEY") || "default-pgp-key"
);
}
// --- Argon2 Hashing --- // --- Argon2 Hashing ---
async hashPassword(password: string): Promise<string> { async hashPassword(password: string): Promise<string> {