"use strict";
/*
 * @adonisjs/hash
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bcrypt = void 0;
/// <reference path="../../adonis-typings/hash.ts" />
const format_1 = __importDefault(require("@phc/format"));
const phc_bcrypt_1 = __importDefault(require("phc-bcrypt"));
/**
 * Generates and verifies hash using Bcrypt as underlying
 * algorigthm.
 */
class Bcrypt {
    constructor(config) {
        this.config = config;
        this.ids = ['bcrypt'];
        this.params = { rounds: 'r' };
        this.version = 98;
    }
    /**
     * Returns hash for a given value
     */
    make(value) {
        return phc_bcrypt_1.default.hash(value, this.config);
    }
    /**
     * Verify hash to know if two values are same.
     */
    verify(hashedValue, plainValue) {
        return phc_bcrypt_1.default.verify(hashedValue, plainValue);
    }
    /**
     * Returns a boolean telling if hash needs a rehash. Returns true when
     * one of the original params have been changed.
     */
    needsReHash(value) {
        const deserialized = format_1.default.deserialize(value);
        /**
         * Phc formatted Bycrpt hash
         */
        if (deserialized.id === 'bcrypt') {
            if (this.version !== deserialized.version) {
                return true;
            }
            return !!Object.keys(this.params).find((key) => {
                return deserialized.params[this.params[key]] !== this.config[key];
            });
        }
        /**
         * Re-format non phc formatted bcrypt hashes.
         */
        if (value.startsWith('$2b') || value.startsWith('$2a')) {
            return true;
        }
        throw new Error('value is not a bcrypt hash');
    }
}
exports.Bcrypt = Bcrypt;
