<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Encrypt Class — PHP 8+ Stub
 * A biblioteca original usava mcrypt (removida no PHP 8.0).
 * Esta é uma versão stub segura que não causa crash.
 * O sistema usa Auth.php com openssl para criptografia real.
 */
class CI_Encrypt {
    public function __construct()
    {
        log_message('debug', 'Encrypt Class (stub PHP8) Initialized');
    }

    public function encode(string $string, string $key = ''): string
    {
        $key = $key ?: config_item('encryption_key');
        $iv  = random_bytes(16);
        $enc = openssl_encrypt($string, 'AES-256-CBC', substr(hash('sha256', $key, true), 0, 32), OPENSSL_RAW_DATA, $iv);
        return base64_encode($iv . $enc);
    }

    public function decode(string $string, string $key = ''): string
    {
        $key  = $key ?: config_item('encryption_key');
        $data = base64_decode($string);
        if (strlen($data) < 17) return '';
        $iv  = substr($data, 0, 16);
        $enc = substr($data, 16);
        return openssl_decrypt($enc, 'AES-256-CBC', substr(hash('sha256', $key, true), 0, 32), OPENSSL_RAW_DATA, $iv) ?: '';
    }

    public function set_cipher(string $cipher): static { return $this; }
    public function set_mode(string $mode): static { return $this; }
    public function encode_from_legacy(string $string, string $legacy_iv = '', string $key = ''): string
    {
        return $this->encode($string, $key);
    }
}
