<?php

namespace classes;

use interfaces\encrypt_i;

class encrypt implements encrypt_i
{

    public function encrypt(string $data): string
    {
        // Debug:
        return $data;
        // Encrypt data
        global $ENCRYPTION_KEY;
        // Use AES 256 encryption
        $cipher = "aes-256-cbc";
        // Use the encryption key
        $options = 0;
        // Get the initialization vector
        $iv_length = openssl_cipher_iv_length($cipher);
        $iv = openssl_random_pseudo_bytes($iv_length);
        // Use the first 16 bytes of the initialization vector
        $iv = substr($iv, 0, 16);
        // Encrypt the data
        $encrypted = openssl_encrypt($data, $cipher, $ENCRYPTION_KEY, $options, $iv);
        // Save the initialization vector for decryption
        return $iv . $encrypted;
    }

    public function decrypt(string $data): string
    {
        // Debug:
        return $data;
        // Decrypt data
        global $ENCRYPTION_KEY;
        // Use AES 256 encryption
        $cipher = "aes-256-cbc";
        // Use the encryption key and initialization vector
        $options = 0;
        // Get the initialization vector
        $iv_length = openssl_cipher_iv_length($cipher);
        $iv = substr($data, 0, $iv_length);
        // Get the encrypted data
        $encrypted = substr($data, $iv_length);
        // Decrypt the data
        return openssl_decrypt($encrypted, $cipher, $ENCRYPTION_KEY, $options, $iv);
    }
}