Files
pcpuma_unam_api/src/bcrypt/bcrypt.service.ts
T

32 lines
852 B
TypeScript

import * as bcrypt from 'bcrypt';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class BcryptService {
constructor(private configService: ConfigService) {}
comparar(password: string, passwordDb: string) {
return bcrypt.compareSync(password, passwordDb);
}
encriptar(password: string) {
const salt = bcrypt.genSaltSync(
parseInt(this.configService.get<string>('SALT_ROUNDS')),
);
return bcrypt.hashSync(password, salt);
}
generarPassword = () => {
const length = 10;
const charset =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-@#$%&=?¿*()/_-';
let password = '';
for (let i = 0; i < length; i++)
password += charset.charAt(Math.floor(Math.random() * charset.length));
return password;
};
}