Introduce repository pattern across multiple services, including `favorites`, `tags`, `sessions`, `reports`, `auth`, and more. Decouple crypto functionalities into modular services like `HashingService`, `JwtService`, and `EncryptionService`. Improve testability and maintainability by simplifying dependencies and consolidating utility logic.
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import type * as jose from "jose";
|
|
import { EncryptionService } from "./services/encryption.service";
|
|
import { HashingService } from "./services/hashing.service";
|
|
import { JwtService } from "./services/jwt.service";
|
|
import { PostQuantumService } from "./services/post-quantum.service";
|
|
|
|
/**
|
|
* @deprecated Use HashingService, JwtService, EncryptionService or PostQuantumService directly.
|
|
* This service acts as a Facade for backward compatibility.
|
|
*/
|
|
@Injectable()
|
|
export class CryptoService {
|
|
constructor(
|
|
private readonly hashingService: HashingService,
|
|
private readonly jwtService: JwtService,
|
|
private readonly encryptionService: EncryptionService,
|
|
private readonly postQuantumService: PostQuantumService,
|
|
) {}
|
|
|
|
async hashEmail(email: string): Promise<string> {
|
|
return this.hashingService.hashEmail(email);
|
|
}
|
|
|
|
async hashIp(ip: string): Promise<string> {
|
|
return this.hashingService.hashIp(ip);
|
|
}
|
|
|
|
getPgpEncryptionKey(): string {
|
|
return this.encryptionService.getPgpEncryptionKey();
|
|
}
|
|
|
|
async hashPassword(password: string): Promise<string> {
|
|
return this.hashingService.hashPassword(password);
|
|
}
|
|
|
|
async verifyPassword(password: string, hash: string): Promise<boolean> {
|
|
return this.hashingService.verifyPassword(password, hash);
|
|
}
|
|
|
|
async generateJwt(
|
|
payload: jose.JWTPayload,
|
|
expiresIn = "2h",
|
|
): Promise<string> {
|
|
return this.jwtService.generateJwt(payload, expiresIn);
|
|
}
|
|
|
|
async verifyJwt<T extends jose.JWTPayload>(token: string): Promise<T> {
|
|
return this.jwtService.verifyJwt<T>(token);
|
|
}
|
|
|
|
async encryptContent(content: string): Promise<string> {
|
|
return this.encryptionService.encryptContent(content);
|
|
}
|
|
|
|
async decryptContent(jwe: string): Promise<string> {
|
|
return this.encryptionService.decryptContent(jwe);
|
|
}
|
|
|
|
async signContent(content: string): Promise<string> {
|
|
return this.encryptionService.signContent(content);
|
|
}
|
|
|
|
async verifyContentSignature(jws: string): Promise<string> {
|
|
return this.encryptionService.verifyContentSignature(jws);
|
|
}
|
|
|
|
generatePostQuantumKeyPair() {
|
|
return this.postQuantumService.generatePostQuantumKeyPair();
|
|
}
|
|
|
|
encapsulate(publicKey: Uint8Array) {
|
|
return this.postQuantumService.encapsulate(publicKey);
|
|
}
|
|
|
|
decapsulate(cipherText: Uint8Array, secretKey: Uint8Array) {
|
|
return this.postQuantumService.decapsulate(cipherText, secretKey);
|
|
}
|
|
}
|