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.
33 lines
869 B
TypeScript
33 lines
869 B
TypeScript
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);
|
|
}
|
|
}
|