Я делаю это так:
import {Entity, model, property} from '@loopback/repository';
const crypto = require('crypto');
@model()
export class User extends Entity{
[...]
@property({
type: 'string',
required: false
})
private hashedPassword: string;
@property({
type: 'string'
})
private salt: string;
set password(password: string){
if(!this.salt || !this.salt.length){
this.salt = crypto.randomBytes(32).toString('hex');
}
this.hashedPassword = this.encryptPassword(password);
}
private encryptPassword(password: string) {
return crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
};
public checkPassword(password: string) {
return this.encryptPassword(password) === this.hashedPassword;
};
}