<?php

declare(strict_types=1);

/** 
 * @author Robert Strutts <Robert@TryingToScale.com>
 * @copyright Copyright (c) 2022, Robert Strutts.
 * @license https://mit-license.org/
 */

namespace prj\mockup\controllers\app;

class enc_ctrl {
  public $page_output;
  private $key = "51623a064210f9d3d5162d0885621f53";
  
  private function get_roles(): string {
      $roles = ['roles'=>'admin, super, rw', 'name'=>'Bob'];
      return json_encode($roles);
  }
  
  private function show_roles(bool|string $in): void {
      echo "<br>" . PHP_EOL;
      if ($in === false) {
          echo "Invalid cypher text";
      } else {
        var_dump(json_decode($in, true));
      }
      echo "<br>" . PHP_EOL;
  }

  private function time_enc(array $levels): void {
      $enc = new \tts\encryption($this->key);
      foreach($levels as $level) {
        echo "ENC: Level = $level <br>" . PHP_EOL;  
        $enc->change_security_level($level);
        
//        $key = $enc->generate_valid_key();
        echo "KEY Used: $this->key <br>" . PHP_EOL;
        
        $startTime = microtime(true);
        $data = $enc->encrypt($this->get_roles());
        echo "<br> $data <br>" . PHP_EOL;    
        echo "Encrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
        
        $startTime = microtime(true);
        $decoded = $enc->decrypt($data);
        $this->show_roles($decoded);
        echo "Decrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";    
        echo "<hr><br>".PHP_EOL;
      }
  }
  
  /* 
   * Let's test the in-secure, Quicker encryption methods
   * These methods are good engough to make sure 
   * data was not tampered with and hide it's contents 
   * from spying eyes.
   */
  public function index() {
      $levels = ['lighting','blaze','quick','good'];
      $this->time_enc($levels);
  }

  // Let's test the secure encryption methods.
  public function slow() {
      $levels = ['good','normal','paranoid'];
      $this->time_enc($levels);
  }
  
  public function sodium() { // 0.0041 Seconds
    $key = "oH5LUubQXkUgC2sXkxahLnzgcZokVFANyxAew+kOvuk=";
    $crypto = new \tts\crypto($key);
//    echo $crypto->a_single_key_maker();
    
    
    $startTime = microtime(true);
    $data = $crypto->encrypt($this->get_roles());
    echo "<br> $data <br>" . PHP_EOL;    
    echo "Encrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
        
//    $data = "sJWIPEasG1W8ModZJKU8jOR78/i1qUZDl6nX5ew21ceOdEZHNDGwqCA1lD70GuIHP4DJEMQ=";
    
    $startTime = microtime(true);
    $decoded = $crypto->decrypt($data);
    $this->show_roles($decoded);
    
    echo "Decrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";    
    echo "<hr><br>".PHP_EOL;
  }
  
  public function storage() {
      $key = "78a5011b9997cd03a28a3412c66565b7c32715b35e055d7abfc228236308d3b2";
      $sc = new \tts\sodium_storage($key);
      
      $startTime = microtime(true);
      $encoded = $sc->encrypt($this->get_roles());
      echo "<br> $encoded <br>" . PHP_EOL;    
      echo "Encrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
    
    try {
        $startTime = microtime(true);
        $plain_text = $sc->decrypt($encoded);
        
        $this->show_roles($plain_text);  
        echo "Decrypt Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";    
        echo "<hr><br>".PHP_EOL;
    } catch (Exception $ex) {
        // Handle the exception here
        echo $ex->getMessage();
    }
  }
  
  public function sodium_pwd_test() { // 0.1233 Seconds
        $startTime = microtime(true);
        $c = new \tts\password_storage();
//        $key = $c->generate_a_key();
        $key = "9054695cc0f4080c15865de470f63ee993478b14c357eb910c815a4e0a1d4401";

        $h = $c->hash("HelpMe", $key);
        var_dump( $c->verify("HelpMe", $h, $key) );
        echo "Hashed PWD and Verified it: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
  }

  public function pwd_test() { //  0.9281 Seconds
        $startTime = microtime(true);
        $db_hash = \tts\security::do_password_hash("HelpMe");
        var_dump( \tts\security::do_password_verify("HelpMe", $db_hash) );
        echo "Hashed PWD and Verified it: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
  }

  
}