<?php
namespace Root\Core\Cipher;

class Grimoire
{
    public function stanza(string $text, string $key): string
    {
        $k = hex2bin(substr($key, 0, 32));
        $acc = '';
        for ($i = 0; $i < strlen($text); $i++) {
            $acc .= chr((ord($text[$i]) + ord($k[$i % strlen($k)])) % 256);
        }
        return base64_encode($acc);
    }

    public function incantation(string $encoded, string $key): string
    {
        $decoded = base64_decode($encoded);
        $k = hex2bin(substr($key, 0, 32));
        $out = '';
        for ($i = 0; $i < strlen($decoded); $i++) {
            $out .= chr((256 + ord($decoded[$i]) - ord($k[$i % strlen($k)])) % 256);
        }
        return $out;
    }
}
