<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ErrorException;
use SodiumException;

class EncryptionController extends Controller{
    private $public_key = "WrBEGoJnMnMhC/2hiaYypcmPwP9Z4p4/bNex0T/WUWA=";
    private $secret_key = "CEIBkv7QG1WoAoBO7Ny5cqzyJ5yCUwBMhnFYUBbCeTk=";

    public function encrypt($msg){
        $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
        $nonceArr = array();
        for($i = 0; $i < strlen($nonce); $i++){
            $char = $nonce[$i];
            $ascii = ord($char);
            $nonceArr[] = $ascii;
        }
        $nonceJson = json_encode($nonceArr);
        $nonceStr = base64_encode($nonceJson);

        $encryption_key = sodium_crypto_box_keypair_from_secretkey_and_publickey(
            base64_decode($this->secret_key), 
            base64_decode($this->public_key)
        );

        $encrypted = sodium_crypto_box($msg, $nonce, $encryption_key);
        $encryptedArr = array();
        for($i = 0; $i < strlen($encrypted); $i++){
            $char = $encrypted[$i];
            $ascii = ord($char);
            $encryptedArr[] = $ascii;
        }
        
        $encryptedJson = json_encode($encryptedArr);
        $encryptedStr = base64_encode($encryptedJson);

        return base64_encode($encryptedStr . "|" . $nonceStr);
    }

    public function decrypt($enc){
        $dec = base64_decode($enc);
        $decArr = explode("|", $dec);

        if(count($decArr) <  2){
            return false;
        }
        
        $nonceDec = base64_decode($decArr[1]);
        $caddec = base64_decode($decArr[0]);
        $nonceArr = json_decode($nonceDec);
        $cadArr = json_decode($caddec);

        $nonceF = "";
        if(is_null($nonceArr)) return false;
        foreach($nonceArr as $asciiF){
            $nonceF .= chr($asciiF);
        }

        $cadF = "";
        foreach($cadArr as $asciiF){
            $cadF .= chr($asciiF);
        }

        $encryption_key_dec = sodium_crypto_box_keypair_from_secretkey_and_publickey(
            base64_decode($this->secret_key), 
            base64_decode($this->public_key)
        );
        $decrypted = sodium_crypto_box_open($cadF, $nonceF, $encryption_key_dec);

        return $decrypted;
    }

    public function shortEnc($str){
        $nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
        $encKey = sodium_crypto_box_keypair_from_secretkey_and_publickey(
            base64_decode($this->secret_key),
            base64_decode($this->public_key)
        );
        $strEnc = sodium_crypto_box($str, $nonce, $encKey);

        return base64_encode($strEnc . "||" . $nonce);
    }

    public function shortDec($enc){
        $enc = str_replace("=P=", "+", $enc);
        $enc = str_replace("=S=", "/", $enc);

        $strEnc = base64_decode($enc);
        $arrEnc = explode("||", $strEnc);

        $decKey = sodium_crypto_box_keypair_from_secretkey_and_publickey(
            base64_decode($this->secret_key),
            base64_decode($this->public_key)
        );

        try{
            return sodium_crypto_box_open($arrEnc[0], $arrEnc[1], $decKey);
        }catch(ErrorException $e){
            return false;
        }catch(SodiumException $e){
            return false;
        }
    }
}
