<?php

namespace App\Services;

class IntegrationSignature
{
    public function sign(array $payload, ?string $secret): ?string
    {
        if (blank($secret)) {
            return null;
        }

        return hash_hmac('sha256', json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), $secret);
    }

    public function verify(array $payload, ?string $secret, ?string $signature): bool
    {
        if (blank($secret) || blank($signature)) {
            return false;
        }

        return hash_equals((string) $this->sign($payload, $secret), (string) $signature);
    }
}
