<?php

namespace App\Services\ProductOwner;

class RequestSigner
{
    public function sign(string $payload, string $secret): string
    {
        return hash_hmac('sha256', $payload, $secret);
    }

    public function verify(string $payload, string $signature, string $secret): bool
    {
        return hash_equals($this->sign($payload, $secret), $signature);
    }

    /**
     * @param  array<string, mixed>  $data
     */
    public function signPayload(array $data, string $secret): string
    {
        ksort($data);

        return $this->sign(json_encode($data, JSON_THROW_ON_ERROR), $secret);
    }
}
