"use strict";
/*
 * @poppinss/utils
 *
 * (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.safeEqual = void 0;
const buffer_alloc_1 = __importDefault(require("buffer-alloc"));
const crypto_1 = require("crypto");
/**
 * Generates a random string for a given size
 */
function safeEqual(value, comparisonValue) {
    if (typeof value === 'string' && typeof comparisonValue === 'string') {
        /**
         * The length of the main value to ensure that we compare equal strings
         * against each other to get a constant time.
         */
        const expectedLength = Buffer.byteLength(value);
        /**
         * Value A
         */
        const valueBuffer = (0, buffer_alloc_1.default)(expectedLength, 0, 'utf-8');
        valueBuffer.write(value);
        /**
         * Value B
         */
        const comparisonValueBuffer = (0, buffer_alloc_1.default)(expectedLength, 0, 'utf-8');
        comparisonValueBuffer.write(comparisonValue);
        /**
         * Ensure values are same and also have same length
         */
        return ((0, crypto_1.timingSafeEqual)(valueBuffer, comparisonValueBuffer) &&
            expectedLength === Buffer.byteLength(comparisonValue));
    }
    return (0, crypto_1.timingSafeEqual)(Buffer.from(value), Buffer.from(comparisonValue));
}
exports.safeEqual = safeEqual;
