forked from Yara724/api
Changed bcrypt to scrypt built-in
This commit is contained in:
@@ -1,14 +1,26 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import * as bcrypt from "bcrypt";
|
||||
import * as crypto from "node:crypto"
|
||||
|
||||
@Injectable()
|
||||
export class HashService {
|
||||
async hash(password: string): Promise<string> {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
return bcrypt.hash(password, salt);
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async compare(password: string, hash: string) {
|
||||
return bcrypt.compare(password, 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"));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user