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

32 lines
852 B
TypeScript
Raw Normal View History

2022-06-24 02:00:17 -05:00
import * as bcrypt from 'bcrypt';
2022-04-21 17:49:08 -05:00
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class BcryptService {
constructor(private configService: ConfigService) {}
2022-07-05 21:53:47 -05:00
comparar(password: string, passwordDb: string) {
return bcrypt.compareSync(password, passwordDb);
}
2022-04-21 17:49:08 -05:00
encriptar(password: string) {
const salt = bcrypt.genSaltSync(
2022-05-02 14:40:17 -05:00
parseInt(this.configService.get<string>('SALT_ROUNDS')),
2022-04-21 17:49:08 -05:00
);
return bcrypt.hashSync(password, salt);
}
2022-07-12 16:20:54 -05:00
generarPassword = () => {
const length = 10;
2022-07-12 16:20:54 -05:00
const charset =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-@#$%&=?¿*()/_-';
2022-07-12 16:20:54 -05:00
let password = '';
for (let i = 0; i < length; i++)
2022-07-24 22:35:00 -05:00
password += charset.charAt(Math.floor(Math.random() * charset.length));
2022-07-12 16:20:54 -05:00
return password;
};
2022-04-21 17:49:08 -05:00
}