Files
yara724api/src/utils/hash/hash.service.ts
2026-05-25 16:44:43 +03:30

26 lines
813 B
TypeScript

import { Injectable } from "@nestjs/common";
import * as crypto from "node:crypto"
@Injectable()
export class HashService {
hash(password: string): Promise<string> {
return new Promise((resolve, reject) => {
const salt = crypto.randomBytes(16).toString("hex");
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
if (err) reject(err);
const hash = `${salt}:${derivedKey.toString("hex")}`;
resolve(hash);
});
});
}
compare(password: string, hash: string): Promise<boolean> {
return new Promise((resolve, reject) => {
const [salt, key] = hash.split(":");
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
if (err) reject(err);
resolve(key === derivedKey.toString("hex"));
});
});
}
}