Как зашифровать пароль в loopback 4?

Предполагая, что вы не пытаетесь сделать что-то злонамеренное, все, что вы хотели бы сделать с вашими собственными URL-адресами, может быть запущено с помощью htaccess .

1
задан Manish Balodia 21 January 2019 в 04:19
поделиться

1 ответ

Я делаю это так:

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;
  };
}
0
ответ дан angelwally 21 January 2019 в 04:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: