feat: add modular services and repositories for improved code organization
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.
This commit is contained in:
32
backend/src/crypto/services/hashing.service.ts
Normal file
32
backend/src/crypto/services/hashing.service.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { hash, verify } from "@node-rs/argon2";
|
||||
|
||||
@Injectable()
|
||||
export class HashingService {
|
||||
async hashEmail(email: string): Promise<string> {
|
||||
const normalizedEmail = email.toLowerCase().trim();
|
||||
return this.hashSha256(normalizedEmail);
|
||||
}
|
||||
|
||||
async hashIp(ip: string): Promise<string> {
|
||||
return this.hashSha256(ip);
|
||||
}
|
||||
|
||||
async hashSha256(text: string): Promise<string> {
|
||||
const data = new TextEncoder().encode(text);
|
||||
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 hashPassword(password: string): Promise<string> {
|
||||
return hash(password, {
|
||||
algorithm: 2,
|
||||
});
|
||||
}
|
||||
|
||||
async verifyPassword(password: string, hash: string): Promise<boolean> {
|
||||
return verify(hash, password);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user