"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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.safeEqual = void 0;
const buffer_1 = require("buffer");
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_1.Buffer.byteLength(value);
        /**
         * Value A
         */
        const valueBuffer = buffer_1.Buffer.alloc(expectedLength, 0, 'utf-8');
        valueBuffer.write(value);
        /**
         * Value B
         */
        const comparisonValueBuffer = buffer_1.Buffer.alloc(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_1.Buffer.byteLength(comparisonValue));
    }
    return (0, crypto_1.timingSafeEqual)(buffer_1.Buffer.from(value), buffer_1.Buffer.from(comparisonValue));
}
exports.safeEqual = safeEqual;
