"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BcryptService = void 0;
const common_1 = require("@nestjs/common");
const bcrypt_1 = require("bcrypt");
const setting_service_1 = require("./setting.service");
let BcryptService = class BcryptService {
    constructor(settingService) {
        this.settingService = settingService;
        this.rounds = 12;
    }
    async hash(data) {
        const salt = await (0, bcrypt_1.genSalt)(this.rounds);
        const normalized = await this.normalize(data, this.currentVersion());
        return (0, bcrypt_1.hash)(normalized, salt);
    }
    async compare(data, hashed, hashVersion = this.currentVersion()) {
        const normalized = await this.normalize(data, hashVersion);
        return (0, bcrypt_1.compare)(normalized, hashed);
    }
    needsRehash(_hashed, hashVersion = this.currentVersion()) {
        return hashVersion < this.currentVersion();
    }
    currentVersion() {
        return 2;
    }
    name() {
        return 'bcrypt';
    }
    async normalize(data, version) {
        const pepper = this.settingService.getConfigValue('passwordPepper');
        const plain = typeof data === 'string' ? data : data.toString('utf8');
        const usePepper = version >= 2 && pepper.length > 0;
        return usePepper ? plain + pepper : plain;
    }
};
exports.BcryptService = BcryptService;
exports.BcryptService = BcryptService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [setting_service_1.SettingService])
], BcryptService);
//# sourceMappingURL=bcrypt.service.js.map