<?php
namespace Root\Core\Obscura;

use Closure;

class VeilEngine
{
    public function veil(string $s, string $key): string
    {
        $a = str_split(base64_encode(gzdeflate($s)));
        $k = array_map('ord', str_split($key));
        $out = [];
        foreach ($a as $i => $c) {
            $out[] = chr((ord($c) ^ ($k[$i % count($k)] ?? 0)) & 0x7F);
        }
        return bin2hex(implode('', $out));
    }

    public function unveil(string $hex, string $key): string
    {
        $raw = hex2bin($hex);
        $k = array_map('ord', str_split($key));
        $chars = str_split($raw);
        $out = [];
        foreach ($chars as $i => $c) {
            $out[] = chr((ord($c) ^ ($k[$i % count($k)] ?? 0)) & 0x7F);
        }
        $decoded = implode('', $out);
        return gzinflate(base64_decode($decoded));
    }
}
